import sys from typing import List def clamp(v: int, l: int, r: int): return max(l, min(v, r)) def main(n: int, m: int, l: List[int], r: List[int]): if not (sum(l) <= m <= sum(r)): return -1 if m == sum(l): return (m * m - sum(e * e for e in l)) // 2 tl, tr = 0, 10 ** 9 + 10 while abs(tr - tl) > 1: t = (tl + tr) // 2 sum_t = 0 for i in range(n): sum_t += clamp(t, l[i], r[i]) if sum_t < m: tl = t else: tr = t t0, sum_t0 = tl, 0 for i in range(n): sum_t0 += clamp(t0, l[i], r[i]) d = m - sum_t0 x = 0 for i in range(n): c = clamp(t0, l[i], r[i]) if d > 0 and l[i] <= t0 < r[i]: c += 1 d -= 1 x += c * c return (m * m - x) // 2 if __name__ == '__main__': readline = sys.stdin.readline t = int(readline()) answers = [] for _ in range(t): n, m = map(int, readline().split()) l = list(map(int, readline().split())) r = list(map(int, readline().split())) answers.append(main(n, m, l, r)) print('\n'.join(map(str, answers)))