#include <bits/stdc++.h>
using namespace std;
pair<int, int> v[200005];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> v[i].first;
    for (int i = 1; i <= n; i++) cin >> v[i].second;
    long long sum = 0;
    long long sol = LONG_MAX;
    sort(v + 1, v + n + 1);
    for (int i = n; i >= 0; i--) {
      sol = min(sol, max(1LL * v[i].first, sum));
      sum += v[i].second;
    }
    cout << sol << '\n';
  }
}
