#include <bits/stdc++.h>
using namespace std;
long long int hcf(long long int a, long long int b) {
  if (b == 0) return a;
  return hcf(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / hcf(a, b);
}
bool isprime(long long int n) {
  if (n == 1) {
    return false;
  }
  for (long long int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
void onlinejudge() {}
void solve() {
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int> > v(n);
  for (long long int i = 0; i <= n - 1; i++) cin >> v[i].first;
  for (long long int i = 0; i <= n - 1; i++) cin >> v[i].second;
  sort(v.begin(), v.end());
  long long int sum = 0;
  long long int ans = 1e18;
  for (long long int i = n - 1; i >= -1; i--) {
    if (i == n - 1)
      ans = v[i].first;
    else if (i == -1) {
      sum += v[i + 1].second;
      ans = min(ans, sum);
    } else {
      sum += v[i + 1].second;
      ans = min(ans, max(v[i].first, sum));
    }
  }
  cout << ans << "\n";
}
int main() {
  onlinejudge();
  long long int ttt;
  ttt = 1;
  cin >> ttt;
  while (ttt--) solve();
  return 0;
}
