/**
 *    author:  tourist
 *    created: 09.11.2024 01:45:38
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> x(n);
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    vector<int> y(n);
    for (int i = 0; i < n; i++) {
      cin >> y[i];
    }
    bool fail = false;
    for (int i = 0; i < n; i++) {
      if (x[i] > y[i]) {
        fail = true;
      }
    }
    vector<int> idx(n);
    iota(idx.begin(), idx.end(), 0);
    sort(idx.begin(), idx.end(), [&](int i, int j) {
      return y[i] > y[j];
    });
    for (int i = 0; i < n - 1; i++) {
      if (x[idx[i]] < x[idx[i + 1]]) {
        fail = true;
      }
    }
    vector<int> d(n);
    for (int i = 0; i < n; i++) {
      d[i] = y[idx[i]] - x[idx[i]];
    }
    for (int i = 0; i < n - 1; i++) {
      if (d[i] < d[i + 1]) {
        fail = true;
      }
    }
    cout << (fail ? -1 : d[0]) << '\n';
  }
  return 0;
}
