#include <bits/stdc++.h>
using namespace std;
pair<int, int> arr[1000007];
long long cs[1000007];
long long ps[1000007];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> arr[i].first;
    for (int i = 1; i <= n; i++) cin >> arr[i].second;
    sort(arr + 1, arr + 1 + n);
    int f = 0;
    int p = 0;
    int prev = 0;
    for (int i = n; i >= 1; i--) {
      if (prev >= arr[i].first) continue;
      if (arr[i].first <= prev + arr[i].second) {
        prev = arr[i].first;
      } else {
        prev += arr[i].second;
      }
    }
    cout << prev << endl;
  }
  return 0;
}
