#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<long long> a(n), b(n);
    long long sumb = 0;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i], sumb += b[i];
    vector<pair<long long, long long> > arr;
    for (int i = 0; i < n; i++) {
      arr.push_back({a[i], b[i]});
    }
    sort(arr.begin(), arr.end());
    long long dp[200005] = {};
    for (int i = n - 1; i >= 0; i--) {
      dp[i] = dp[i + 1] + arr[i].second;
    }
    long long ans = 1e18;
    for (int i = n - 1; i >= 0; i--) {
      long long temp = max(arr[i].first, dp[i + 1]);
      ans = min(ans, temp);
    }
    ans = min(ans, sumb);
    cout << ans << "\n";
  }
}
