#include <bits/stdc++.h>
using namespace std;
long long n, G[200005], S[200005];
pair<long long, long long> P[200005];
bool cmp(pair<long long, long long> p1, pair<long long, long long> p2) {
  return p1.first < p2.first;
}
void solve() {
  cin >> n;
  long long a, b;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    P[i].first = a;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b;
    P[i].second = b;
  }
  sort(P + 1, P + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    S[i] = S[i - 1] + P[i].second;
    G[i] = max(G[i - 1], P[i].first);
  }
  long long ans = LLONG_MAX;
  for (int i = 0; i <= n; i++) {
    if (P[i].first >= ans) break;
    if (i != n && P[i + 1].first == P[i].first) continue;
    ans = min(ans, max(P[i].first, S[n] - S[i]));
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
