from heapq import heappush, heappop 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())) ttl = sum(L) if m < ttl or sum(R) < m: print(-1) continue que = [] for i in range(n): if L[i] < R[i]: heappush(que, (L[i], i)) while ttl < m: l, i = heappop(que) L[i] += 1 ttl += 1 if L[i] < R[i]: heappush(que, (L[i], i)) ans = sum(l * (m - l) for l in L) // 2 print(ans)