#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <numeric>
#include <random>
#include <chrono>
#include <ctime>

using namespace std;

#define forn(i,n) for(int i=0; i<n; ++i)

void solve() {
    int n;
    cin >> n;
    vector<int> h(n);
    for(int i = 0; i < n; ++i){
        cin >> h[i];
    }
    vector<int> p(n);
    for(int i = 0; i < n; ++i){
        cin >> p[i];
    }
    swap(p,h);
    for(int i = 0; i < n; ++i){
        if(h[i] < p[i]){
            cout << "-1\n";
            return;
        }
    }
    map<int,int> ind;
    vector<int>index;
    for(int i = 0; i < n; ++i){
        if(ind[h[i]] == 0) {ind[h[i]]=i+1; index.push_back(i);}
        if(p[i] != p[ind[h[i]]-1]){
            cout << "-1\n";
            return;
        }
    }
    if(index.size() == 1){
        cout << h[0] - p[0] << "\n";
        return;
    }
    sort(index.begin(),index.end(),[&](int a, int b){
        return (h[a]>h[b]);
    });
    int prev = INT_MAX;
    for(int i = 0; i < index.size()-1; ++i){
        if(p[index[i]] < p[index[i+1]]){
            cout <<"-1\n";
            return;
        }
    }
    for(int i = 0; i < index.size(); ++i){
        if(h[index[i]] - p[index[i]] > prev){
            cout << "-1\n";
            return;
        }
        prev = h[index[i]] - p[index[i]];
    }
    cout << h[index[0]] - p[index[0]] << "\n";


}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout.setf(ios::fixed);
    cout.precision(20);
    int t = 1;
    cin>>t;
    while (t--) solve();
}
