#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long test;
  cin >> test;
  while (test--) {
    long long n, x, total = 0;
    cin >> n;
    vector<long long> a;
    vector<long long> b;
    for (long long i = 0; i < n; i++) {
      cin >> x;
      a.push_back(x);
    }
    for (long long i = 0; i < n; i++) {
      cin >> x;
      b.push_back(x);
    }
    vector<pair<long long, long long> > p;
    for (long long i = 0; i < n; i++) p.push_back(make_pair(a[i], b[i]));
    sort(p.begin(), p.end());
    long long sum = 0, cost, mini = 1000000001;
    for (long long i = n - 1; i >= 0; i--) {
      cost = max(p[i].first, sum);
      sum += p[i].second;
      mini = min(mini, cost);
    }
    mini = min(mini, sum);
    cout << mini << endl;
  }
  return 0;
}
