#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    int64_t sm = 0;
    for (int i = 0; i < n; ++i) {
      cin >> b[i];
      sm += b[i];
    }
    vector<pair<int64_t, int64_t>> v(n);
    for (int i = 0; i < n; ++i) {
      v[i] = {a[i], b[i]};
    }
    sort(v.begin(), v.end());
    int64_t ans = sm;
    for (int i = 0; i < n; ++i) {
      sm -= v[i].second;
      int64_t e = v[i].first > sm ? v[i].first : sm;
      ans = e < ans ? e : ans;
    }
    cout << ans << '\n';
  }
}
