#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    long long int n, sum = 0, x;
    cin >> n;
    int a[n + 5], b[n + 5];
    for (int j = 0; j < n; j++) {
      cin >> a[j];
    }
    for (int j = 0; j < n; j++) {
      cin >> b[j];
      sum += b[j];
    }
    vector<pair<int, int> > vect;
    for (int j = 0; j < n; j++) vect.push_back(make_pair(a[j], b[j]));
    sort(vect.begin(), vect.end());
    long long int y = sum;
    for (int j = 0; j < n; j++) {
      sum -= vect[j].second;
      if (vect[j].first > sum)
        x = vect[j].first;
      else
        x = sum;
      if (x < y) y = x;
    }
    cout << y << endl;
  }
  return 0;
}
