#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1) os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const deque<T>& v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1) os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "[";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin()) os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
  for (auto it : v) os << it.first << " : " << it.second << "\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
void go() { cin.tie(nullptr)->sync_with_stdio(false); }
bool can(long long mid, vector<long long>& a, vector<long long>& b) {
  long long c = 0;
  for (long long i = 0; i < a.size(); i++) {
    if (a[i] > mid) {
      c += b[i];
    }
  }
  return c <= mid;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n), b(n);
  for (long long& x : a) cin >> x;
  for (long long& x : b) cin >> x;
  long long lo = 0, hi = 1e9 + 7, mid, res = 1e9 + 7;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (can(mid, a, b)) {
      res = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << res << '\n';
}
signed main() {
  go();
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
