#include <bits/stdc++.h>
const int SIZE = 1e5;
using namespace std;
struct coordinate {
  int x, y;
};
bool not_div(int n, vector<int> v) {
  for (int d : v) {
    if (n % d == 0) return false;
  }
  return true;
}
void solver() {
  int n, index;
  std::cin >> n;
  vector<int> result;
  for (size_t i = 4 * n; i >= 1; i -= 2) result.push_back(i);
  for (size_t i = 0; i < n; i++) cout << result[i] << " ";
  cout << endl;
}
int c() {
  int n;
  int cur = 0;
  cin >> n;
  vector<pair<int, int>> arr(n);
  for (size_t i = 0; i < n; i++) cin >> arr[i].first;
  for (size_t i = 0; i < n; i++) cin >> arr[i].second;
  sort(arr.begin(), arr.end(), greater<pair<int, int>>());
  for (size_t i = 0; i < n; i++) {
    if (cur >= arr[i].first) return cur;
    cur += arr[i].second;
    if (cur >= arr[i].first) return min(cur, arr[i].first);
  }
  return cur;
}
int main(int argc, char const *argv[]) {
  int t = 1;
  cin >> t;
  while (t--) {
    cout << c() << endl;
  }
  return 0;
}
