#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> a[200005];
long long suf[200005];
int n;
int main() {
  ios::sync_with_stdio();
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> a[i].first;
    }
    for (int i = 0; i < n; ++i) {
      cin >> a[i].second;
    }
    sort(a, a + n);
    suf[n] = 0;
    for (int i = n - 1; i >= 0; --i) {
      suf[i] = suf[i + 1] + a[i].second;
    }
    long long ans = suf[0];
    for (int i = 0; i < n; ++i) {
      ans = min(ans, max(a[i].first, suf[i + 1]));
    }
    cout << ans << '\n';
  }
  return 0;
}
