#include <bits/stdc++.h>
using namespace std;
const long long N = 2 * 1e5 + 5;
const long long INF = 1e9 + 7;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first > b.first;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long> > a(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i].first;
    }
    for (long long i = 0; i < n; i++) {
      cin >> a[i].second;
    }
    sort(a.begin(), a.end(), comp);
    long long cnt = 0;
    long long ans = a[0].first;
    for (long long i = 0; i < n; i++) {
      if (i == n - 1 && cnt + a[i].second <= max(cnt, a[i].first)) {
        ans = cnt + a[i].second;
      }
      if (i != n - 1 &&
          max(cnt + a[i].second, a[i + 1].first) <= max(cnt, a[i].first)) {
        ans = max(cnt + a[i].second, a[i + 1].first);
        cnt += a[i].second;
      } else
        break;
    }
    cout << ans << '\n';
  }
  return 0;
}
