#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long sum;
    vector<long long> a(n);
    vector<long long> b(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (long long i = 0; i < n; i++) {
      cin >> b[i];
    }
    long long s = 0;
    vector<pair<long long, long long>> v;
    vector<long long> v1;
    for (long long i = 0; i < n; i++) {
      v.push_back(make_pair(a[i], b[i]));
    }
    sort(v.begin(), v.end());
    v1.push_back(v[n - 1].first);
    int i = n - 2;
    int j = n - 1;
    while (j >= 0) {
      s += v[j].second;
      if (i >= 0) {
        v1.push_back(max(v[i].first, s));
      }
      i--;
      j--;
    }
    v1.push_back(s);
    sort(v1.begin(), v1.end());
    cout << v1[0] << "\n";
  }
}
