#include <bits/stdc++.h>
using namespace std;
long long int mod(long long int x) {
  return (((x) % 1000000007) + 1000000007) % 1000000007;
}
long long int mod1(long long int x, long long int m) {
  return (((x) % m) + m) % m;
}
bool check(long long int m, long long int a[], long long int b[],
           long long int n) {
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) {
    if (m < a[i]) sum += b[i];
  }
  return sum <= m;
}
long long int solve() {
  long long int n;
  cin >> n;
  long long int a[n];
  long long int b[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> b[i];
  }
  long long int l = 1, h = 10e9;
  long long int ans = -1;
  while (l < h) {
    long long int m = l + (h - l) / 2;
    if (check(m, a, b, n)) {
      ans = m;
      h = m;
    } else {
      l = m + 1;
    }
  }
  cout << ans << "\n";
  return 0;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
