import sys input = sys.stdin.readline 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 = [min(max(L[i], k), R[i]) for i in range(n)] ttl = sum(cnt) for i in range(n): if ttl == m: break if cnt[i] == k < R[i]: cnt[i] += 1 ttl += 1 ans = sum(cnt[i] * (m - cnt[i]) for i in range(n)) print(ans // 2)