#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    unsigned long long bcount = 0, n, k;
    cin >> n;
    vector<unsigned long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<unsigned long long> b(n);
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      bcount += b[i];
    }
    vector<pair<unsigned long long, unsigned long long>> vec;
    for (int i = 0; i < n; i++) vec.push_back(make_pair(a[i], b[i]));
    sort(vec.begin(), vec.end());
    vector<unsigned long long> ans;
    ans.push_back(bcount);
    for (int i = 0; i < n; i++) {
      bcount -= vec[i].second;
      ans.push_back(max(vec[i].first, bcount));
    }
    cout << *min_element(ans.begin(), ans.end()) << "\n";
  }
  return 0;
}
