#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> a[200001];
void solve() {
  long long n;
  cin >> n;
  long long cnt = 0;
  for (int i = 0; i < n; i++) cin >> a[i].first;
  for (int i = 0; i < n; i++) cin >> a[i].second, cnt += a[i].second;
  sort(a, a + n);
  long long ans = cnt;
  for (int i = 0; i < n; i++) {
    cnt -= a[i].second;
    long long helper = max(cnt, a[i].first);
    ans = min(ans, helper);
  }
  cout << ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << endl;
  }
}
