#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 9;
long long a[maxn], b[maxn], f[maxn], c[maxn], n;
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i <= n; i++) {
    cin >> b[i];
  }
  vector<pair<long long, long long> > pp;
  for (long long i = 1; i <= n; i++) {
    pp.push_back(make_pair(a[i], b[i]));
  }
  sort(pp.begin(), pp.end());
  long long an = 1e18;
  long long sum = 0;
  for (long long i = n; i >= 0; i--) {
    an = min(an, max(sum, i > 0 ? pp[i - 1].first : 0LL));
    if (i > 0) sum += pp[i - 1].second;
  }
  printf("%lld\n", an);
}
signed main() {
  long long T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
