#include <bits/stdc++.h>
using namespace std;
void A() {
  vector<long long int> primes = {
      2,   3,   5,   7,   11,  13,  17,  19,  23,  29,  31,  37,
      41,  43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,
      97,  101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
      157, 163, 167, 173, 179, 181, 191, 193, 197, 199};
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    for (long long int i = 0; i < n; i++) {
      cout << 4 * n - 2 * i << " ";
    }
    cout << '\n';
  }
}
void B() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int a, b;
    cin >> a >> b;
    string city;
    cin >> city;
    long long int n = city.size();
    long long int count = 0;
    long long int last = -1;
    long long int coins = 0;
    for (long long int i = 0; i < n; i++) {
      bool isBomb = (city[i] - '0') == 1;
      if (!isBomb) {
        count++;
      } else {
        if (count > 0 && a > count * b && last != -1) {
          coins += count * b;
        } else if (count > 0 && last != -1) {
          coins += a;
        }
        last = i;
        count = 0;
      }
    }
    if ((count > 0 && last != -1) || last == n - 1) {
      coins += a;
    }
    cout << coins << '\n';
  }
}
void C() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> as(n);
    for (long long int i = 0; i < n; i++) {
      long long int next;
      cin >> next;
      as[i] = next;
    }
    map<long long int, vector<long long int>> times;
    for (long long int i = 0; i < n; i++) {
      long long int next;
      cin >> next;
      times[as[i]].push_back(next);
    }
    long long int mintime = prev(times.end())->first;
    long long int manualtime = 0;
    n = times.size();
    times[0] = vector<long long int>();
    auto it = times.end();
    for (long long int i = 0; i < n; i++) {
      it--;
      long long int sum = 0;
      for (long long int j = 0; j < (it->second).size(); j++) {
        sum += (it->second)[j];
      }
      manualtime += sum;
      mintime = min(mintime, max(prev(it)->first, manualtime));
    }
    cout << mintime << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  C();
  return 0;
}
