#include <bits/stdc++.h> #define rep(i,j,k) for (int i = (j); i <= (k); i ++) #define per(i,j,k) for (int i = (j); i >= (k); i --) #define uint unsigned int #define ll long long #define ull unsigned long long #define db double #define ldb long double #define mkp make_pair #define eb emplace_back #define e_ emplace #define ls (p << 1) #define rs (p << 1 | 1) //#define int ll using namespace std; const int N = 2e5 + 5; const int INF = 0x3f3f3f3f; const ll LINF = 4e18; struct xx { ll a, b, c; }; int n; ll k; xx e[N];
bool check (ll x) { ll used = 0; rep (i, 0, n - 1) { ll a = e[i].a, b = e[i].b, c = e[i].c, sum = a + b + c; if (sum >= x) continue; ll s; if (a > b || a > c || b > c) s = x - sum; else { if (a == c) return false; ll y = LINF; if (a < c) y = min (y, b - a + 1); if (a < b) y = min (y, c - b + 1); s = x - sum + 2 * y; } if (s > k - used) return false; used += s; } return true; }
signed main () { ios::sync_with_stdio (0); cin.tie (0); int T; cin >> T; while (T --) { cin >> n >> k; ll l = LINF, r = LINF; rep (i, 0, n - 1) { cin >> e[i].a >> e[i].b >> e[i].c; ll sum = e[i].a + e[i].b + e[i].c; l = min (l, sum); r = min (r, sum + k); } while (l < r) { ll mid = l + (r - l + 1) / 2; if (check (mid)) l = mid; else r = mid - 1; } cout << l << '\n'; } return 0; }