#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1LL << 60;
const int MAX_N = 1000005;
vector<vector<int> > adj;
vector<bool> vis;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<pair<long long int, long long int> > v(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i], v[i] = {a[i], b[i]};
  sort(v.begin(), v.end());
  vector<long long int> sum(n + 1, 0);
  for (int i = n - 1; i >= 0; i--) sum[i] = sum[i + 1] + v[i].second;
  long long int ans = INF;
  for (int i = 0; i < n; i++) {
    ans = min(ans, max(v[i].first, sum[i + 1]));
  }
  cout << min(ans, sum[0]) << "\n";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
