#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
inline bool equals(long double a, long double b) { return fabs(a - b) < 1e-9; }
inline long long gcd(long long a, long long b) {
  a = abs(a);
  b = abs(b);
  while (a > 0 && b > 0) (a > b ? a %= b : b %= a);
  return (a == 0 ? b : a);
}
inline long long power(long long x, long long n, long long m = LLONG_MAX) {
  long long res = 1;
  x = (x % m + m) % m;
  while (n) {
    if (n & 1) res = (res * x) % m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
void solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long> > v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i].first;
  }
  for (long long i = 0; i < n; i++) {
    cin >> v[i].second;
  }
  sort(v.begin(), v.end(), greater<pair<long long, long long> >());
  long long ans = v[0].first;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    sum += v[i].second;
    long long res = sum;
    if (i < (n - 1)) res = max(v[i + 1].first, sum);
    ans = min(ans, res);
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long i = 1; i <= t; ++i) {
    solve();
  };
  return 0;
}
