#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > v;
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first == b.first) a.second < b.second;
  return a.first > b.first;
}
int main() {
  long long t, n, arr[200005], brr[200005], i;
  cin >> t;
  while (t--) {
    cin >> n;
    for (i = 0; i < n; i++) cin >> arr[i];
    for (i = 0; i < n; i++) cin >> brr[i];
    for (i = 0; i < n; i++) v.push_back({arr[i], brr[i]});
    sort(v.begin(), v.end(), cmp);
    long long self = 0;
    long long deli = v[0].first;
    long long mx = max(self, deli);
    long long mn = deli;
    for (i = 1; i < n; i++) {
      self += v[i - 1].second;
      deli = v[i].first;
      mx = max(self, deli);
      mn = min(mn, mx);
    }
    mn = min(mn, self + v[n - 1].second);
    cout << mn << endl;
    v.clear();
  }
  return 0;
}
