def main(): def cal(S, T, l, r): l -= 1 r += 1 rep = 0 if T[0] < S[0]: if l == -1: rep += 1 else: x = S[0]-l y = T[0]-l #print(x, y, 0) while y < x: rep += 1 x = (x+1)//2 S[0] = T[0] if T[-1] > S[-1]: if r == -1: rep += 1 else: x = r-S[-1] y = r-T[-1] #print(x, y, 1) while y < x: rep += 1 x = (x+1)//2 S[-1] = T[-1] for i in range(len(S)-1): if S[i] < T[i] and T[i+1] < S[i+1]: l0, r0, l1, r1 = S[i], S[i+1], T[i], T[i+1] c = min(l1-l0, r0-r1) f = 1 le = r1-l1 i = 0 m = (c+le-1)//le if l1-l0 <= r0-r1: f = 0 while l0 < l1 or r1 < r0: rep += 1 f1 = 0 if m >> i & 1: f1 = 1 if f ^ f1: l1 = max(l0, 2*l1-r1) else: r1 = min(r0, 2*r1-l1) i += 1 elif S[i] < T[i]: x = T[i+1]-S[i] y = T[i+1]-T[i] #print(x, y, 2) while y < x: rep += 1 x = (x+1)//2 elif T[i+1] < S[i+1]: x = S[i+1]-T[i] y = T[i+1]-T[i] #print(x, y, 3) while y < x: rep += 1 x = (x+1)//2 return rep n, m = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [(a, b) for a, b in zip(A, B)] C.sort() T = [] S = [] l = 0 ans = 0 for i in range(m): if C[i][0] == C[i][1]: if T: #print(S) #print(T) if sorted(T) == T and len(T) == len(set(T)) and l <= T[0] and T[-1] < C[i][1]: ans += cal(S, T, l, C[i][1]-1) pass else: return -1 l = C[i][1]+1 T = [] S = [] continue T.append(C[i][1]) S.append(C[i][0]) if T: #print(S) #print(T) if sorted(T) == T and len(T) == len(set(T)) and l <= T[0]: ans += cal(S, T, l, -2) return ans else: return ans return -1 for _ in range(int(input())): print(main())