#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 12;
int cs = 0;
int n;
int a[maxn], b[maxn];
int ok(int s) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > s) sum += b[i];
  }
  return sum <= s;
}
void run() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  long long l = 1, r = 1e15, ans;
  while (l < r) {
    int mid = l + r >> 1;
    if (ok(mid)) {
      ans = mid;
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}
int main() {
  std::ios::sync_with_stdio(0);
  int T;
  cin >> T;
  while (T--) {
    cs++;
    run();
  }
}
