#include <bits/stdc++.h>
using namespace std;
long long big = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<unsigned long long, unsigned long long>> vec(n);
    for (long long i = 0; i < n; i += 1) {
      cin >> vec[i].first;
    }
    for (long long i = 0; i < n; i += 1) {
      cin >> vec[i].second;
    }
    sort(vec.begin(), vec.end());
    unsigned long long cur = 0;
    bool b = false;
    unsigned long long ret = -1;
    for (long long i = n - 1; !b && i >= 0; i -= 1) {
      if (cur + vec[i].second > vec[i].first) {
        b = true;
        ret = max(cur, vec[i].first);
      } else
        cur += vec[i].second;
    }
    if (b)
      cout << ret << '\n';
    else
      cout << cur << '\n';
  }
  return 0;
}
