#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool ylmin(T &a, T b) {
  return a < b ? 0 : (a = b, 1);
}
template <class T>
inline bool ylmax(T &a, T b) {
  return a > b ? 0 : (a = b, 1);
}
template <class T>
inline T abs(T x) {
  return x < 0 ? -x : x;
}
inline char read() {
  static const int IO_LEN = 1024 * 1024;
  static char buf[IO_LEN], *ioh, *iot;
  if (ioh == iot) {
    iot = (ioh = buf) + fread(buf, 1, IO_LEN, stdin);
    if (ioh == iot) return -1;
  }
  return *ioh++;
}
template <class T>
inline void read(T &x) {
  static int iosig;
  static char ioc;
  for (iosig = 0, ioc = read(); !isdigit(ioc); ioc = read())
    if (ioc == '-') iosig = 1;
  for (x = 0; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0');
  if (iosig) x = -x;
}
const int MAXN = 2e5 + 10;
long long T, n, a[MAXN], b[MAXN], id[MAXN];
inline bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
  for (read(T); T; T--) {
    read(n);
    for (int i = (1); i <= (n); i++) read(a[i]), id[i] = i;
    for (int i = (1); i <= (n); i++) read(b[i]);
    sort(id + 1, id + n + 1, cmp);
    long long sum = 0, ans = 1e9;
    for (int i = (1); i <= (n); i++) sum += b[i];
    for (int i = (0); i <= (n); i++) {
      sum -= b[id[i]];
      ylmin(ans, max(a[id[i]], sum));
    }
    cout << ans << '\n';
  }
  return 0;
}
