#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int t, n;
pair<int, int> b[N];
long long tong, kq;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> b[i].first;
    for (int i = 1; i <= n; i++) cin >> b[i].second;
    sort(b + 1, b + 1 + n, greater<pair<int, int> >());
    kq = b[1].first, tong = 0;
    b[n + 1] = {0, 0};
    for (int i = 1; i <= n + 1; i++) {
      kq = min(kq, max(tong, (long long)b[i].first));
      tong += b[i].second;
    }
    cout << kq << '\n';
  }
  return 0;
}
