#include <bits/stdc++.h>
using namespace std;
bool compare(pair<long long int, long long int>& p1,
             pair<long long int, long long int>& p2) {
  if (p1.first > p2.first)
    return true;
  else if (p1.first < p2.first)
    return false;
  else
    return p1.second <= p2.second;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<long long int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    map<long long int, long long int, greater<long long int>> mp;
    for (int i = 0; i < n; i++) {
      mp[a[i]] += b[i];
    }
    long long int ans = 1e15;
    long long int prev1 = 0;
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
      ans = min(ans, max(prev1, itr->first));
      prev1 += itr->second;
    }
    ans = min(ans, prev1);
    cout << ans << "\n";
  }
  return 0;
}
