#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long double pii = 3.14159265359;
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 200001;
int N;
bool calc(int T, vector<int>& A, vector<int>& B) {
  long long sum = 0;
  for (int x = 0; x < (N); x++) {
    if (A[x] > T) {
      sum += B[x];
      if (sum > T) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int Test_case;
  cin >> Test_case;
  while (Test_case-- != 0) {
    cin >> N;
    vector<int> A(N), B(N);
    for (auto& i : A) cin >> i;
    for (auto& i : B) cin >> i;
    int ans = INT_MAX;
    int l = 1, r = 1000000000;
    while (l <= r) {
      int m = l + (r - l) / 2;
      if (calc(m, A, B)) {
        ckmin(ans, m);
        r = m - 1;
      } else
        l = m + 1;
    }
    cout << ans << nl;
  }
  return 0;
}
