#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> courier(n), self(n);
    for (int i = 0; i < n; ++i) cin >> courier[i];
    for (int i = 0; i < n; ++i) cin >> self[i];
    vector<pair<long long, long long>> combined(n);
    for (int i = 0; i < n; ++i) combined[i] = {courier[i], self[i]};
    sort(combined.rbegin(), combined.rend());
    long long sum_self = 0, sum_courier = 0;
    for (int i = 0; i < n; ++i) {
      if (sum_self + combined[i].second < combined[i].first)
        sum_self += combined[i].second;
      else {
        sum_courier = combined[i].first;
        break;
      }
    }
    cout << max(sum_self, sum_courier) << "\n";
  }
}
