#include <bits/stdc++.h>
using namespace std;
long long int areTest = 1;
void foobar() {
  long long int n, i;
  cin >> n;
  vector<long long int> arr(n), brr(n);
  for (long long int &x : arr) {
    cin >> x;
  }
  for (long long int &x : brr) {
    cin >> x;
  }
  vector<pair<long long int, long long int> > crr(n);
  for (long long int i = 0; i < n; ++i) {
    crr[i] = {arr[i], brr[i]};
  }
  sort(crr.begin(), crr.end());
  long long int mx = crr[n - 1].first;
  long long int s = crr[n - 1].second;
  long long int ans = mx;
  for (long long int i = n - 2; i >= 0; i--) {
    long long int m = max(crr[i].first, s);
    ans = min(ans, m);
    s = s + crr[i].second;
  }
  ans = min(ans, s);
  cout << ans << endl;
}
int main() {
  long long int test = 1;
  if (areTest) {
    cin >> test;
  }
  while (test--) foobar();
}
