#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int nax = 2e5 + 200;
const double mpi = acos(-1);
const double eps = 1e-10;
int dr[] = {1, -1, 0, 0, 1, -1, -1, 1};
int dc[] = {0, 0, 1, -1, 1, 1, -1, -1};
int a[nax], b[nax], n;
long long mid, cont;
bool f() {
  cont = 0;
  for (int i = 0; i < (int)n; ++i) {
    if (a[i] > mid) {
      cont += b[i];
    }
  }
  return cont <= mid;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(20) << fixed;
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < (int)n; ++i) cin >> a[i];
    for (int i = 0; i < (int)n; ++i) cin >> b[i];
    long long l = 0, r = 2e14;
    while (l + 1 < r) {
      mid = (l + r) / 2;
      if (f()) {
        r = mid;
      } else {
        l = mid;
      }
    }
    cout << r << '\n';
  }
}
