#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int arr[MAXN];
int b[MAXN];
bool visited[MAXN];
set<int> s1;
vector<pair<int, int>> v1;
vector<int> res;
map<int, int> m1;
int n;
bool check(int x) {
  for (int i = 1; i <= n; i++) {
    visited[i] = false;
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] <= x) {
      visited[i] = true;
    }
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      res += b[i];
    }
  }
  return res <= x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> arr[i];
    }
    for (int i = 1; i <= n; i++) {
      cin >> b[i];
    }
    int l = 0;
    int r = 1e9;
    int ans = 0;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (check(mid)) {
        r = mid - 1;
        ans = mid;
      } else {
        l = mid + 1;
      }
    }
    cout << ans << endl;
  }
}
