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