#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[100005];
vector<bool> vis(100005, false);
long long isprime(long long n) {
  if (n < 2) return 0;
  if (n < 4) return 1;
  if (n % 2 == 0 or n % 3 == 0) return 0;
  for (long long i = 5; i * i <= n; i += 6)
    if (n % i == 0 or n % (i + 2) == 0) return 0;
  return 1;
}
void pairsort(long long a[], long long b[], long long n) {
  pair<long long, long long> pairt[n];
  for (long long i = 0; i < n; i++) {
    pairt[i].first = a[i];
    pairt[i].second = b[i];
  }
  sort(pairt, pairt + n);
  for (long long i = 0; i < n; i++) {
    a[i] = pairt[i].first;
    b[i] = pairt[i].second;
  }
}
long long solve() {
  long long n;
  cin >> n;
  long long cou[n];
  long long take[n];
  for (long long i = 0; i < n; i++) cin >> cou[i];
  for (long long i = 0; i < n; i++) cin >> take[i];
  vector<pair<long long, long long>> v;
  for (long long i = 0; i < n; i++) {
    v.push_back({cou[i], take[i]});
  }
  sort(v.begin(), v.end());
  long long sum = 0;
  long long ans = v[n - 1].first;
  for (long long i = n - 1; i >= 0; i--) {
    ans = min(ans, max(sum, v[i].first));
    sum += v[i].second;
  }
  ans = min(ans, sum);
  cout << ans;
  return 0;
}
int32_t main() {
  long long q = 1;
  cin >> q;
  while (q--) solve(), cout << "\n";
  return 0;
}
