import sys sys.setrecursionlimit(5*10**5) input = sys.stdin.readline from collections import defaultdict, deque, Counter from heapq import heappop, heappush from bisect import bisect_left, bisect_right from math import gcd def sol(): n,m = map(int,input().split()) l = list(map(int,input().split())) r = list(map(int,input().split())) if sum(l) > m or sum(r) < m: print(-1) return cnt = defaultdict(int) left = m for i in range(n): cnt[l[i]] += 1 left -= l[i] r.sort() r = r[::-1] all = [] for k, v in cnt.items(): all.append([k,v]) nn = len(all) all.sort() all.append([10**10,0]) ans = [] for i in range(nn): now = all[i][0] cnt = all[i][1] while r and r[-1] == now: r.pop() cnt -= 1 ans.append([now, 1]) nxt = all[i+1][0] while r and r[-1] < nxt: num = r.pop() dif = num - now if cnt == 0: continue if left > dif * cnt: left -= dif*cnt cnt -= 1 now = num ans.append([now, 1]) else: add = left//cnt rr = left % cnt ans.append([now+add+1,rr]) ans.append([now+add,cnt-rr]) left = 0 cnt = 0 dif = nxt - now if cnt: if left > dif * cnt: left -= dif*cnt else: add = left//cnt rr = left % cnt ans.append([now+add+1,rr]) ans.append([now+add,cnt-rr]) left = 0 if now + add + 1 == nxt: cnt = rr ans.append([now+add,cnt-rr]) elif now + add + 1 == nxt: cnt -= rr else: cnt = 0 ans.append([now+add+1,rr]) ans.append([now+add,cnt-rr]) all[i+1][1] += cnt ansnum = m*(m-1)//2 for i,j in ans: ansnum -= j*(i*(i-1)//2) print(ansnum) return T = int(input()) for i in range(T): sol()