def bis(ok, ng): def is_ok(md): ttl = sum(min(max(L[i], md), R[i]) for i in range(n)) return ttl <= m while abs(ok - ng) > 1: md = (ng + ok) // 2 if is_ok(md): ok = md else: ng = md return ok T = int(input()) for _ in range(T): n, m = map(int, input().split()) L = list(map(int, input().split())) R = list(map(int, input().split())) if m < sum(L) or sum(R) < m: print(-1) continue k = max(R) if sum(R) == m else bis(min(L), max(R)) cnt = sorted([min(max(L[i], k), R[i]), i] for i in range(n)) ttl = sum(cnt[i][0] for i in range(n)) i = 0 while ttl < m: if cnt[i][0] < R[cnt[i][1]]: cnt[i][0] += 1 ttl += 1 i += 1 ans = sum(cnt[i][0] * (m - cnt[i][0]) for i in range(n)) // 2 print(ans)