#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long a[n][2];
  for (int i = 0; i < n; i++) cin >> a[i][0];
  for (int i = 0; i < n; i++) cin >> a[i][1];
  vector<pair<long long, long long>> v;
  for (int i = 0; i < n; i++) {
    v.push_back(make_pair(a[i][0], a[i][1]));
  }
  sort(v.begin(), v.end());
  long long sum = 0, ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    ans = min(ans, max(v[n - 1 - i].first, sum));
    sum += v[n - 1 - i].second;
  }
  cout << min(sum, ans) << endl;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
