#include <bits/stdc++.h>
using namespace std;
long long a[200001], b[200001];
int n;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    long long sum = 0LL;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      sum += b[i];
    }
    vector<pair<long long, long long> > c;
    for (int i = 0; i < n; i++) {
      c.push_back(make_pair(a[i], i));
    }
    sort(c.begin(), c.end());
    long long ans = sum;
    for (int i = 0; i < n; i++) {
      sum -= b[c[i].second];
      ans = min(ans, max(sum, c[i].first));
    }
    cout << ans << '\n';
  }
  return 0;
}
