#include <bits/stdc++.h>
using namespace std;
int cnt[100000 + 1];
const int mod = 1e9 + 7;
vector<int> v;
void solve() {
  int n;
  cin >> n;
  vector<pair<long long int, long long int>> v;
  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];
  for (int i = 0; i < n; i++) {
    v.push_back({a[i], b[i]});
  }
  sort(v.rbegin(), v.rend());
  long long int sum = 0;
  long long int res = v[0].first;
  for (int i = 0; i < v.size(); i++) {
    if (sum <= v[i].first) {
      sum += v[i].second;
      if (sum >= v[i].first) {
        cout << v[i].first << endl;
        return;
      }
      long long int mn = max(v[i].first, sum);
      res = min(res, mn);
    } else {
      break;
    }
  }
  cout << min(sum, res) << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
