#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e18;
void solve() {
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int>> a(n);
  for (long long int i = 0; i < n; ++i) cin >> a[i].first;
  for (long long int i = 0; i < n; ++i) cin >> a[i].second;
  sort(a.begin(), a.end());
  vector<long long int> suff(n);
  suff[n - 1] = a[n - 1].second;
  long long int ans = inf;
  ans = min(ans, a[n - 1].first);
  for (long long int i = n - 2; i >= 0; --i) {
    suff[i] = suff[i + 1] + a[i].second;
    ans = min(ans, max(a[i].first, suff[i + 1]));
  }
  ans = min(ans, suff[0]);
  cout << ans << '\n';
}
int32_t main() {
  long long int tst;
  cin >> tst;
  while (tst--) solve();
}
