#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<long long int, long long int>> v;
    long long int a[n], b[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i];
    }
    for (int i = 0; i < n; i++) {
      pair<int, int> p;
      p.first = a[i];
      p.second = b[i];
      v.push_back(p);
    }
    sort(v.begin(), v.end());
    long long int sum = 0;
    long long int ans = v[n - 1].first;
    for (int i = 0; i < n; i++) {
      long long int ans1;
      if (i != n - 1) {
        sum = sum + v[n - i - 1].second;
        ans1 = max(sum, v[n - i - 2].first);
      } else {
        sum = sum + v[n - i - 1].second;
        ans1 = sum;
      }
      ans = min(ans, ans1);
    }
    cout << ans << endl;
  }
}
