#include <bits/stdc++.h>
using namespace std;
bool possible(vector<int> a, vector<int> b, int x) {
  long long sum = 0;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] > x) sum += b[i];
  }
  if (sum <= x) return 1;
  return 0;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    int low = 1, high = 1e9 + 1;
    while (low < high) {
      int m = (low + high) / 2;
      if (possible(a, b, m))
        high = m;
      else
        low = m + 1;
    }
    cout << low << endl;
  }
}
