#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vc = vector<char>;
using vd = vector<double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
int a[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int b[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
signed main() {
  ll t;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    vll a(n);
    vll b(n);
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (ll i = 0; i < n; i++) {
      cin >> b[i];
    }
    vector<pll> v;
    for (ll i = 0; i < n; i++) {
      v.push_back({a[i], b[i]});
    }
    sort(v.begin(), v.end());
    vll x(n);
    x[n - 1] = v[n - 1].second;
    for (ll i = n - 2; i >= 0; i--) {
      x[i] = x[i + 1] + v[i].second;
    }
    ll ans = INT_MAX;
    for (ll i = 0; i < n - 1; i++) {
      ll p = v[i].first;
      if (p < x[i + 1]) {
        p += abs(p - x[i + 1]);
      }
      ans = min(ans, p);
    }
    ll r = (accumulate((b).begin(), (b).end(), 0ll));
    ans = min(ans, v[n - 1].first);
    cout << min(ans, r) << "\n";
  }
}
