#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long int inf64 = 1e18 + 5;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t, n, i;
  cin >> t;
  while (t--) {
    cin >> n;
    int a[n], b[n];
    for (i = 0; i < n; i++) cin >> a[i];
    for (i = 0; i < n; i++) cin >> b[i];
    auto chk = [&](long long int mid) {
      long long int sum = 0;
      for (int i = 0; i < n; i++) {
        if (a[i] > mid) sum += b[i];
      }
      return (sum <= mid);
    };
    long long int first = 0, l = 1e15, ans = 1e15;
    while (first <= l) {
      long long int mid = (first + l) / 2;
      if (chk(mid)) {
        ans = min(ans, mid);
        l = mid - 1;
      } else
        first = mid + 1;
    }
    cout << ans << '\n';
  }
}
