#include <bits/stdc++.h>
using namespace std;
const long long N = 200001;
vector<long long> prime(555, 1);
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  long long mx = 0;
  vector<pair<long long, long long> > v;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
    mx += b[i];
  }
  long long l = 0, r = mx + 1;
  while (l < r) {
    long long mid = (l + r) / 2;
    long long time = 0;
    for (long long i = 0; i < n; i++) {
      if (a[i] <= mid)
        ;
      else
        time += b[i];
    }
    if (time <= mid)
      r = mid;
    else
      l = mid + 1;
  }
  cout << r;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  cin >> t;
  for (long long i = 2; i * i <= 550; i++)
    if (prime[i])
      for (long long j = i * i; j <= 550; j += i) prime[j] = 0;
  for (long long i = 0; i < t; i++) {
    solve();
    cout << "\n";
  }
}
