#include <bits/stdc++.h>
using namespace std;
bool comparing(pair<unsigned long long int, unsigned long long int> a,
               pair<unsigned long long int, unsigned long long int> b) {
  return a.first > b.first;
}
int main() {
  unsigned long long int cycles;
  cin >> cycles;
  for (unsigned long long int cycle = 0; cycle < cycles; ++cycle) {
    unsigned long long int num;
    cin >> num;
    pair<unsigned long long int, unsigned long long int>* arr =
        new pair<unsigned long long int, unsigned long long int>[num];
    for (unsigned long long int a = 0; a < num; ++a) {
      cin >> arr[a].first;
    }
    for (unsigned long long int a = 0; a < num; ++a) {
      cin >> arr[a].second;
    }
    sort(arr, arr + num, comparing);
    unsigned long long int bTime = 0;
    unsigned long long int aTime = arr[0].first;
    unsigned long long int curMin = aTime;
    for (unsigned long long int cur = 0; cur < num; ++cur) {
      if (cur == num - 1) {
        aTime = 0;
      } else {
        aTime = arr[cur + 1].first;
      }
      bTime += arr[cur].second;
      curMin = min(curMin, max(bTime, aTime));
    }
    cout << curMin << endl;
  }
  return 0;
}
