#include <bits/stdc++.h>
using namespace std;
bool func(pair<long long int, long long int> p,
          pair<long long int, long long int> q) {
  if (p.second == q.second) {
    return p.first > q.first;
  }
  return p.second > q.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int t, n, m;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    long long int arr[n], brr[n];
    long long int ans = INT_MIN;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    vector<pair<long long int, long long int>> v;
    for (int i = 0; i < n; i++) {
      cin >> brr[i];
      if (brr[i] >= arr[i]) {
        ans = max(ans, arr[i]);
      } else {
        v.push_back({brr[i], arr[i]});
      }
    }
    sort(v.begin(), v.end(), func);
    long long int x = 0;
    for (int i = 0; i < v.size(); i++) {
      long long int b = v[i].first;
      long long int a = v[i].second;
      if (i == 0) {
        x = b;
        ans = max(ans, b);
      } else {
        if (x + b <= a) {
          x += b;
          ans = max(ans, x);
        } else {
          ans = max(ans, a);
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
