#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5;
int a[200005], b[200005];
int n;
bool check(int x) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > x) sum += b[i];
  }
  return sum <= x;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> 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 = 0, r = 1e9 + 1;
    while (r - l > 1) {
      int mid = l + r >> 1;
      if (check(mid)) {
        r = mid;
      } else
        l = mid;
    }
    cout << r << endl;
  }
  return 0;
}
