#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
using namespace std;
inline long long add(long long a, long long b) {
  return ((a + b) < 1000000007 ? (a + b) : (a + b - 1000000007));
}
int prime[1000005];
int dppk(int kp1) {
  int jk = (int)pow(2, 1);
  for (int ik = 0; ik < (int)pow(jk, 2); ik++) {
    if (kp1 == 1)
      kp1++;
    else
      kp1--;
    kp1 = max(kp1, jk);
  }
  if (kp1 == 1)
    return kp1;
  else
    return 0;
}
int dppk1(int e) {
  int e1, e2, e3;
  e1 = 0;
  e2 = e1;
  e3 += e1 + e2;
  if (e3 == e1) return e3;
}
void seive() {
  prime[1] = 1;
  prime[0] = 1;
  int i = 2;
  while (i <= sqrt(1000005)) {
    if (prime[i] == 1) {
      continue;
    }
    int j = 2 * i;
    while (j <= 1000005) {
      prime[j] = 1;
      j += i;
    }
    i++;
  }
}
inline long long sub(long long a, long long b) {
  return ((a - b) < 0 ? a - b + 1000000007 : a - b);
}
long long power(long long a, long long b) {
  long long y = 1;
  if (b == 0) return y;
  y = power(a, b / 2);
  y = (y * y);
  if (b & 1) y = (a * y);
  return y;
}
using namespace std;
struct hex {};
void primeFactors(int n) {}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long> > v;
    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];
    for (long long i = 0; i < n; i++) v.push_back({a[i], b[i]});
    sort(v.begin(), v.end());
    long long mxt = 0;
    for (long long i = n - 1; i >= 0; i--) {
      if (v[i].second + mxt > v[i].first) {
        mxt = max(v[i].first, mxt);
        break;
      } else {
        mxt += v[i].second;
      }
    }
    cout << mxt << endl;
  }
}
