#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
const int mxn = 2e5;
int n, a[mxn], b[mxn];
bool ok(int t) {
  int second = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= t) continue;
    if (second > t - b[i]) return false;
    second += b[i];
  }
  return true;
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) cin >> b[i];
  int lo = 1, hi = 1e9;
  while (lo < hi) {
    int mid = lo + (hi - lo) / 2;
    ok(mid) ? hi = mid : lo = mid + 1;
  }
  cout << lo << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
