#include <bits/stdc++.h>
using namespace std;
const long long losn = 1e6 + 5;
const long long maxn = 1e5 + 5;
const long long minn = 1e3 + 5;
const long long tiny = 1e2 + 5;
const long long inf = 1e9;
const long long binf = 1e18;
const long long mod = 1e9 + 7;
const long long hmod = 4698571;
const long double pi = 3.1415926;
const long long myn = 0;
void gk() { cout << "---------------" << endl; }
void ck() { cout << 1 << endl; }
void ck(long long a) { cout << a << endl; }
void ck(long long a[minn][minn], long long n, long long m) {
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}
long long n, m, k, T, u, v, len;
struct node {
  long long a, b;
};
node a[maxn * 2];
bool emp(node a, node b) { return a.a > b.a; }
long long as[maxn * 2];
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long maxs = 0;
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i].a;
      as[i] = 0;
    }
    for (long long i = 1; i <= n; i++) {
      cin >> a[i].b;
    }
    sort(a + 1, a + 1 + n, emp);
    for (long long i = 1; i <= n; i++) {
      as[i] += a[i].b + as[i - 1];
    }
    a[n + 1].a = 0;
    long long ans = as[n];
    for (long long i = 1; i <= n + 1; i++) {
      ans = min(ans, max(a[i].a, as[i - 1] - as[0]));
    }
    cout << ans << endl;
  }
  return 0;
}
