#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int s = 0;
    long long int a[n];
    ;
    for (long long int i = 0; i < n; i++) cin >> a[i];
    ;
    long long int b[n];
    ;
    for (long long int i = 0; i < n; i++) cin >> b[i];
    ;
    vector<pair<long long int, long long int>> v;
    for (long long int i = 0; i < n; i++) {
      s += b[i];
      v.push_back({a[i], b[i]});
    }
    sort(v.begin(), v.end());
    long long int sum = 0;
    long long int ans = (long long int)(9e18);
    for (long long int i = n - 1; i >= 0; i--) {
      long long int order = v[i].first;
      long long int take = sum;
      sum += v[i].second;
      long long int curAns = max(order, take);
      ans = min(curAns, ans);
    }
    cout << min(ans, s) << endl;
  }
  return 0;
}
