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