#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N], n;
bool check(int x) {
  long long s = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] > x) s += b[i];
  }
  return s <= 1LL * x;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &b[i]);
    }
    int l = 1, r = a[1];
    for (int i = 2; i <= n; ++i) r = max(r, a[i]);
    while (l < r) {
      int mid = (l + r) >> 1;
      if (check(mid))
        r = mid;
      else
        l = mid + 1;
    }
    printf("%d\n", l);
  }
  return 0;
}
