#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long MAXN = 1e5 + 5;
void solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long> > a(n);
  long long mx = -1, sum = 0;
  for (long long i = 0; i < n; i++) cin >> a[i].first, mx = max(mx, a[i].first);
  for (long long i = 0; i < n; i++) cin >> a[i].second, sum += a[i].second;
  sort(a.begin(), a.end());
  long long ans = min(mx, sum), nsum = 0;
  for (long long i = n - 1; i > 0; i--) {
    nsum += a[i].second;
    ans = min(ans, max(a[i - 1].first, nsum));
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
