#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
const int MAXN = 2e5;
pair<ll, int> arr[MAXN];
ll sum[MAXN];
ll ans = 1e18;
void init() { ans = 1e18; }
void input() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i].first;
  }
  for (int j = 0; j < n; j++) {
    cin >> arr[j].second;
  }
  sort(arr, arr + n);
}
void make_sum() {
  sum[0] = arr[0].second;
  for (int i = 1; i < n; i++) {
    sum[i] = sum[i - 1] + arr[i].second;
  }
  ans = sum[n - 1];
}
void solve() {
  for (int i = 0; i < n; i++) {
    ans = min(ans, max(arr[i].first, sum[n - 1] - sum[i]));
    if (ans == 3) {
    }
  }
}
void output() { cout << ans << endl; }
int main() {
  int number_of_testcases = 1;
  cin >> number_of_testcases;
  while (number_of_testcases--) {
    init();
    input();
    make_sum();
    solve();
    output();
  }
  return 0;
}
