#include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; #define X first #define Y second #define SZ(a) ((int)a.size()) #define ALL(v) v.begin(), v.end() #define pb push_back const ll MAXC = 2e9; int L[200005], R[200005]; void solve() { int n, m; ll sumL = 0, sumR = 0; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> L[i], sumL += L[i]; for (int i = 1; i <= n; ++i) cin >> R[i], sumR += R[i]; if (sumL > m || sumR < m) { cout << "-1\n"; return; } if (sumL < m) { auto cal = [&](ll mid) { ll tar = (mid + 1) / 2; ll res = 0; for (int i = 1; i <= n; ++i) res += min((ll)R[i], max((ll)L[i], tar)); return res; }; ll l = 0, r = MAXC; while (r - l > 1) { ll mid = (l + r) >> 1; if (cal(mid) >= m) r = mid; else l = mid; } ll tar = (l + 1) / 2; sumL = 0; for (int i = 1; i <= n; ++i) { L[i] = min((ll)R[i], max((ll)L[i], tar)); sumL += L[i]; } assert(sumL < m); for (int i = 1; i <= n && sumL < m; ++i) { if (L[i] == tar && L[i] + 1 <= R[i]) { ++L[i]; ++sumL; } } assert(sumL == m); } ll ans = (ll)m * m; for (int i = 1; i <= n; ++i) ans -= (ll)L[i] * L[i]; cout << ans / 2 << "\n"; } int main() { ios::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { solve(); } }