#include <bits/stdc++.h>
using namespace std;
int64_t a[200005], b[200005];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int64_t t;
  cin >> t;
  while (t--) {
    int64_t n;
    cin >> n;
    vector<pair<int64_t, int64_t>> vec;
    for (int64_t i = 1; i <= n; i++) cin >> a[i];
    for (int64_t i = 1; i <= n; i++) cin >> b[i];
    for (int64_t i = 1; i <= n; i++) vec.push_back({a[i], b[i]});
    sort(vec.rbegin(), vec.rend());
    int64_t sum = 0, mi = 0x3f3f3f3f, i = 0;
    while (i < n) {
      auto p = vec[i];
      int64_t a = p.first, b = p.second;
      mi = min(mi, max(a, sum));
      sum += b;
      i++;
    }
    if (i == n) mi = min(mi, sum);
    cout << mi << '\n';
  }
  return 0;
}
