#include <bits/stdc++.h>
using namespace std;
int main() {
  long t, n, i, a[200010], b[200010];
  cin >> t;
  while (t--) {
    cin >> n;
    for (i = 0; i < n; i++) cin >> a[i];
    for (i = 0; i < n; i++) cin >> b[i];
    long long sum = 0, l = 0, r = 1e9, m;
    while (l + 1 < r) {
      m = (l + r) / 2, sum = 0;
      for (i = 0; i < n; i++) {
        if (a[i] > m) {
          sum += b[i];
          if (sum > m) {
            break;
          }
        }
      }
      if (i == n)
        r = m;
      else
        l = m;
    }
    cout << r << endl;
  }
}
