#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long T = 1, n;
  cin >> T;
  while (T--) {
    cin >> n;
    vector<pair<long long, long long> > A(n);
    for (long long i = 0; i < n; ++i) {
      cin >> A[i].first;
      A[i].first *= -1;
    }
    for (long long i = 0; i < n; ++i) {
      cin >> A[i].second;
    }
    sort((A).begin(), (A).end());
    long long ans = -A[0].first;
    long long acum = 0;
    for (long long j = 0; j < n; ++j) {
      acum += A[j].second;
      ans = min(ans, max(acum, j == n - 1 ? 0 : -A[j + 1].first));
    }
    cout << ans << "\n";
  }
}
