#include <iomanip>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <set>
#include "queue"
//#include <multiset>
#include <map>
#include <utility>
#include <numeric>
#include "random"
#ifndef __clang__
#include <bits/stdc++.h>
#endif
using namespace std;

#define all(x) (x).begin(), (x).end()
#define int long long


void solve() {
    int n;
    cin >> n;

    vector<int> a(n);
    vector<int> b(n);

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = 0; i < n; i++)
        cin >> b[i];

    vector<pair<int, int>> to;
    for (int i = 0; i < n; i++) {
        if (a[i] > b[i])
        {
            cout << "-1\n";
            return;
        }
        to.emplace_back(b[i], a[i]);
    }

    sort(to.begin(), to.end());
    int mx = to[0].second, dif = to[0].first - to[0].second;
    for (int i = 1; i < n; i++) {
        if (to[i].first == to[i - 1].first) {
            if (to[i].second == to[i - 1].second)
                continue;
            cout << "-1\n";
            return;
        }
        int now_dif = to[0].first - to[0].second;
        if (mx > to[i].second || dif > now_dif)
        {
            cout << "-1\n";
            return;
        }
        mx = to[i].second;
        dif = now_dif;
    }
    cout << dif << "\n";

}

signed main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}
