#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int cl(int x, int y) { return (x / y) + (x % y != 0); }
void solve() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > v(n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i].first;
    v[i].first *= -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> v[i].second;
  }
  sort(v.begin(), v.end());
  long long sum = 0, flag = 0;
  for (int i = 0; i < n; i++) {
    long long x = -v[i].first;
    ans = max(x, sum);
    sum += v[i].second;
    if (sum >= x) {
      flag = 1;
      break;
    }
  }
  if (!flag) ans = sum;
  cout << ans;
  cout << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
