#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n];
    long long brr[n];
    for (long long i = 0; i < n; i++) cin >> arr[i];
    for (long long i = 0; i < n; i++) cin >> brr[i];
    long long lo = 1;
    long long hi = 1e10;
    long long ans = 1e10;
    while (lo <= hi) {
      long long mid = (lo + hi) / 2;
      long long sum = 0;
      for (long long i = 0; i < n; i++) {
        if (arr[i] <= mid)
          continue;
        else
          sum += brr[i];
      }
      if (sum <= mid) {
        ans = mid;
        hi = mid - 1;
      } else {
        lo = mid + 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
