#include <bits/stdc++.h>
using namespace std;
inline int add(int _a, int _b) {
  if (_a < 0) {
    _a += 1000000007;
  }
  if (_b < 0) {
    _b += 1000000007;
  }
  if (_a + _b >= 1000000007) {
    return _a + _b - 1000000007;
  }
  return _a + _b;
}
inline int mul(int _a, int _b) {
  if (_a < 0) {
    _a += 1000000007;
  }
  if (_b < 0) {
    _b += 1000000007;
  }
  return ((long long int)((long long int)_a * (long long int)_b)) % 1000000007;
}
const int N = 2e5;
int n, a[N + 5], b[N + 5];
bool F(long long int mid) {
  int i, j;
  long long int sum = 0;
  for (i = 0; i < n; ++i) {
    if (a[i] <= mid) continue;
    sum += b[i];
  }
  return (sum <= mid);
}
void solve() {
  int i, j;
  long long int lo = 0, hi = 0;
  for (i = 0; i < n; ++i) hi += a[i];
  ++hi;
  while (lo < hi) {
    long long int mid = (lo + hi) >> 1ll;
    if (F(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  printf("%lld", hi), puts("");
}
int main() {
  int cs, ts;
  scanf("%d", &ts);
  for (cs = 0; cs < ts; cs++) {
    int i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++) scanf("%d", &a[i]);
    for (i = 0; i < n; i++) scanf("%d", &b[i]);
    solve();
  }
}
