#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int> > c;
    long long int a[n + 1], b[n + 1];
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      c.push_back(make_pair(a[i], b[i]));
    }
    sort(c.begin(), c.end());
    long long int time = 0;
    for (int i = c.size() - 1; i >= 0; i--) {
      if (c[i].second + time >= c[i].first) {
        cout << max(c[i].first, time) << endl;
        break;
      }
      time += c[i].second;
      if (i == 0) cout << time << endl;
    }
  }
}
