#include <bits/stdc++.h>
using namespace std;
bool mycomp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, sum = 0;
    cin >> n;
    vector<long long> A(n), B(n);
    for (int i = 0; i < n; i++) {
      cin >> A[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> B[i];
      sum += B[i];
    }
    vector<pair<long long, long long>> v;
    for (int i = 0; i < n; i++) {
      v.push_back(make_pair(A[i], B[i]));
    }
    sort(v.begin(), v.end(), mycomp);
    long long time = 0, min_time = INT_MAX;
    min_time = min(min_time, sum);
    for (int i = 0; i < n; i++) {
      sum -= v[i].second;
      time = max(sum, v[i].first);
      min_time = min(min_time, time);
    }
    cout << min_time << endl;
  }
  return 0;
}
