#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return (a < b ? a : b); }
void solve() {
  long long int n, sum = 0;
  cin >> n;
  vector<long long int> a(n), b(n);
  for (long long int i = 0; i < n; i++) cin >> a[i];
  for (long long int i = 0; i < n; i++) {
    cin >> b[i];
    sum += b[i];
  }
  if (n == 1) {
    cout << min(a[0], b[0]) << endl;
    return;
  }
  long long int s = sum;
  vector<pair<long long int, long long int> > v;
  for (long long int i = 0; i < n; i++) {
    v.push_back({a[i], b[i]});
  }
  sort(v.begin(), v.end());
  if (v[0].first >= s) {
    cout << s << endl;
    return;
  }
  vector<long long int> ans;
  for (long long int i = 0; i < n; i++) {
    sum -= v[i].second;
    ans.push_back(max(v[i].first, sum));
  }
  sort(ans.begin(), ans.end());
  cout << ans[0] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
