#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a == 0) {
    return b;
  } else {
    return gcd(b % a, a);
  }
}
long long int lcm(long long int a, long long int b) {
  return a * b / gcd(a, b);
}
int f(char c) { return (int)c - 96; }
struct cmp {
  bool operator()(const int &a, const int &b) { return a > b; }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int a[200005];
    long long int b[200005];
    long long int sum[200005];
    multimap<long long int, long long int> m;
    for (long long int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    for (long long int i = 1; i <= n; i++) {
      cin >> b[i];
    }
    for (long long int i = 1; i <= n; i++) {
      m.insert({a[i], b[i]});
    }
    int count = 1;
    sum[0] = 0;
    for (auto x : m) {
      sum[count] = sum[count - 1] + x.second;
      count++;
    }
    long long int ans = sum[n];
    count = 2;
    for (auto x : m) {
      ans = min(max((long long int)x.first, sum[n] - sum[count - 1]), ans);
      count++;
    }
    cout << ans << endl;
  }
  return 0;
}
