#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 3e6;
const long long mod = 1e9 + 7;
const long double PI = acos((long double)-1);
long long pw(long long a, long long b, long long md = mod) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = (a * res) % md;
    }
    a = (a * a) % md;
    b >>= 1;
  }
  return (res);
}
int q;
int n;
long long a[maxn];
long long b[maxn];
bool chk(long long x) {
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= x) continue;
    sum += b[i];
  }
  return (sum <= x);
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    long long l = 0, r = 1e9;
    while (r - l > 1) {
      long long mid = (l + r) / 2;
      if (chk(mid))
        r = mid;
      else
        l = mid;
    }
    cout << r << '\n';
  }
  return (0);
}
