#include <bits/stdc++.h>
using namespace std;
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
const long long INF = 1e9 + 7;
const long long mod = (1e9);
const int maxn = (int)3e5;
const long double PI = acos(-1.0);
const long double eps = 0.000000001;
mt19937 rnd(time(0));
vector<long long> a, b;
bool f(long long t) {
  long long ost = 0;
  for (int i = 0; i < int(a.size()); ++i)
    if (a[i] > t) ost += b[i];
  return (ost <= t);
}
void solve() {
  int n;
  cin >> n;
  a.resize(n);
  for (auto &i : a) cin >> i;
  b.resize(n);
  for (auto &i : b) cin >> i;
  long long l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    long long m = r + l >> 1;
    (f(m) ? r : l) = m;
  }
  cout << r << '\n';
}
signed main() {
  GO();
  int Q = 1;
  cin >> Q;
  while (Q--) {
    solve();
  }
}
