#include #include #include #include #include #include int64_t solve(const size_t, const uint64_t m, const std::vector& l, const std::vector& r) { const uint64_t sum_l = std::accumulate(l.begin(), l.end(), uint64_t(0)); const uint64_t sum_r = std::accumulate(r.begin(), r.end(), uint64_t(0)); if (m < sum_l or m > sum_r) { return -1; } std::map inout; for (uint64_t e : l) ++inout[e]; for (uint64_t e : r) --inout[e]; uint64_t x = 0; for (uint64_t e : l) x += e * e; uint64_t sum = sum_l; size_t num = 0; for (uint64_t t0 = 0; ; ++t0) { if (auto it = inout.find(t0); it != inout.end()) { num += it->second; } uint64_t d = std::min(m - sum, num); x += (2 * t0 + 1) * d; sum += d; if (sum == m) { return (m * m - x) / 2; } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); size_t t; std::cin >> t; while (t --> 0) { size_t n; uint64_t m; std::cin >> n >> m; std::vector l(n), r(n); for (auto& e : l) std::cin >> e; for (auto& e : r) std::cin >> e; std::cout << solve(n, m, l, r) << '\n'; } return 0; }