#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<pair<long long, long long>> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i].first;
    for (long long i = 0; i < n; i++) cin >> v[i].second;
    sort(v.begin(), v.end());
    vector<long long> sums(n, 0);
    sums.back() = v.back().second;
    for (int i = n - 2; i >= 0; i--) {
      sums[i] = v[i].second + sums[i + 1];
    }
    long long res = sums.front();
    sums.push_back(0);
    for (int i = 0; i < n; i++) {
      res = min(res, max(v[i].first, sums[i + 1]));
    }
    cout << res << endl;
  }
  return 0;
}
