def solve():
    n = int(input())
    hs = list(map(int, input().split()))
    ps = list(map(int, input().split()))
    diffs = [0] + [p - h for h, p in sorted(zip(hs, ps))]
    print(diffs[-1] if all(diffs[i] <= diffs[i + 1] for i in range(n)) else -1)

for _ in range(int(input())):
    solve()