#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, i, j, k;
    cin >> n;
    int b[200001], a;
    vector<pair<int, int> > pr;
    for (i = 0; i < n; i++) {
      cin >> a;
      pr.push_back({a, i});
    }
    for (i = 0; i < n; i++) {
      cin >> b[i];
    }
    sort(pr.begin(), pr.end());
    int ans = pr[n - 1].first, sum = 0;
    for (i = n - 1; i >= 0; i--) {
      if (sum + b[pr[i].second] < pr[i].first) {
        sum += b[pr[i].second];
        if (i > 0)
          ans = max(sum, pr[i - 1].first);
        else
          ans = sum;
      } else
        break;
    }
    cout << ans << endl;
  }
}
