#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> a(n), b(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (long long i = 0; i < n; i++) {
      cin >> b[i];
    }
    long long l = 0, r = 1e15;
    while (l + 1 < r) {
      long long m = (l + r) / 2;
      long long sumTime = 0;
      for (long long i = 0; i < n; i++) {
        if (a[i] <= m) {
          continue;
        } else {
          sumTime += b[i];
        }
      }
      if (sumTime <= m) {
        r = m;
      } else {
        l = m;
      }
    }
    cout << r << endl;
  }
  return 0;
}
