#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const long long mod = 1e9 + 7;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long pct(long long x) { return __builtin_popcount(x); }
long long bit(long long x) { return 31 - __builtin_clz(x); }
long long cdiv(long long a, long long b) {
  return a / b + !(a < 0 || a % b == 0);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
const long long maxn = 1e5 * 3 + 20;
long long n, _, q, k;
void solve() {
  cin >> _;
  while (_--) {
    cin >> n;
    long long sm = 0;
    vector<long long> a(n);
    vector<long long> b(n);
    for (signed i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (signed i = 0; i < n; i++) {
      cin >> b[i];
      sm += b[i];
    }
    vector<long long> order(n);
    iota(order.begin(), order.end(), 0);
    sort(begin(order), end(order),
         [&a](long long i, long long j) { return a[i] < a[j]; });
    long long ans = sm;
    for (long long x : order) {
      long long tmp = max(sm - b[x], a[x]);
      if (tmp < ans) {
        sm -= b[x];
        ans = tmp;
      }
    }
    cout << ans << endl;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
