#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long powerm(long long a, long long b, long long MOD) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long mod(long long x) { return ((x % MOD + MOD) % MOD); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
long long binarySearch(long long a[], long long low, long long high,
                       long long key) {
  while (low <= high) {
    long long mid = low + ((high - low) / 2);
    if (a[mid] < key) {
      low = mid + 1;
    } else if (a[mid] > key) {
      high = mid - 1;
    } else {
      return mid;
    }
  }
  return -1;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<long long> sieve;
void Sieve(int N) {
  const long long maxn = N + 1;
  sieve.resize(maxn);
  for (long long i = (long long)0; i < (long long)maxn; i++) sieve[i] = i;
  sieve[1] = -1;
  sieve[0] = -1;
  for (long long i = (long long)2; i < (long long)maxn; i++)
    if (i == sieve[i])
      for (long long j = 2 * i; j < maxn; j += i)
        if (sieve[j] == j) sieve[j] = i;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    priority_queue<pair<long long, long long> > z;
    long long n, sum = 0;
    cin >> n;
    long long a[n], b[n];
    for (long long i = (long long)0; i < (long long)n; i++) {
      cin >> a[i];
      z.push({a[i], i});
    }
    for (long long i = (long long)0; i < (long long)n; i++) {
      cin >> b[i];
      sum += b[i];
    }
    long long maxm, minm = z.top().first;
    long long prev = 0, previ = z.top().second;
    z.pop();
    while (!z.empty()) {
      prev += b[previ];
      maxm = max(z.top().first, prev);
      minm = min(minm, maxm);
      previ = z.top().second;
      z.pop();
    }
    minm = min(minm, sum);
    cout << minm << '\n';
  }
  return 0;
}
