#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[200000], b[200000];
bool ok(long long mid) {
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    if (b[i] > mid) sum += a[i];
  }
  return sum <= mid;
}
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (long long i = 0; i < n; i++) cin >> b[i];
    for (long long i = 0; i < n; i++) cin >> a[i];
    long long sum = 0;
    for (long long i = 0; i < n; i++) sum += a[i];
    long long mx = *max_element(b, b + n);
    long long s = 1, e = max(mx, sum);
    long long ans = e;
    while (s <= e) {
      long long mid = s + (e - s) / 2;
      if (ok(mid)) {
        ans = mid;
        e = mid - 1;
      } else
        s = mid + 1;
    }
    cout << ans << "\n";
  }
}
