#include <bits/stdc++.h>
using namespace std;
using pil = pair<long long, long long>;
using pii = pair<int, int>;
using pid = pair<double, double>;
bool sortbysecdesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return a.second < b.second;
}
void solve() {
  int n;
  cin >> n;
  long long a[n + 1];
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<pil> vec1;
  long long ans = LLONG_MAX;
  for (int i = 0; i < n; i++) {
    long long b;
    cin >> b;
    vec1.push_back({a[i], b});
  }
  sort(vec1.begin(), vec1.end());
  for (int i = 1; i < n; i++) {
    vec1[i].second += vec1[i - 1].second;
  }
  for (int i = 0; i < n - 1; i++) {
    long long mx = vec1[i].first;
    if (mx < vec1[n - 1].second - vec1[i].second)
      mx = vec1[n - 1].second - vec1[i].second;
    ans = min(ans, mx);
  }
  ans = min(ans, min(vec1[n - 1].first, vec1[n - 1].second));
  cout << ans << endl;
}
int main() {
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
