def binary_search(ok, ng): while abs(ok - ng) > 1: md = (ng + ok) // 2 S = sum(min(max(l, md), r) for l, r in zip(L, R)) if S <= M: ok = md else: ng = md return ok for _ in range(T := int(input())): N, M = map(int, input().split()) L = list(map(int, input().split())) R = list(map(int, input().split())) if not sum(L) <= M <= sum(R): print(-1); continue k = binary_search(min(L), max(R)+1) K = [min(max(l, k), r) for l, r in zip(L, R)] S = sum(K) for i in range(N): if S == M: break if K[i] == k < R[i]: K[i] += 1; S += 1 print(M*(M-1)//2 - sum(k*(k-1)//2 for k in K))