#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
ll a[N],b[N],c[N];
ll ans;
//ans<=mid返回true;
ll check(ll x){
	ll res = 0;
	int i,j;
	for(i = 1;i<=n;i++){
		if(a[i]<=x){

		}else{
			res += b[i];
		}
	}
//	cout<<x<<" "<<res<<endl;
	ans = min(max(res,x),ans);
	return res;
}

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		ans = 1e18;
		for(i = 1;i<=n;i++){
			cin>>a[i];
			c[i] = a[i];
		}
		sort(c+1,c+1+n);
		for(i = 1;i<=n;i++){
			cin>>b[i];
		}
		int l = 0,r = n;
		while(l<r){
			int mid = (l+r)/2;
			//res<=mid
		//	cout<<l<<" "<<r<<endl;
			ll t = check(c[mid]);
			check(c[l]);
			check(c[r]);
			if(t<=mid)	r = mid;
			else l = mid+1;
		//	cout<<l<<" "<<r<<endl;
		}
		check(l);
		cout<<ans<<endl;

	}

	return 0;
}
/*
2
4
3 7 4 5
2 4 2 4
2
1 2
10 10

*/


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
ll a[N],b[N],c[N];
ll ans;
//ans<=mid返回true;
ll check(ll x){
	ll res = 0;
	int i,j;
	for(i = 1;i<=n;i++){
		if(a[i]<=x){

		}else{
			res += b[i];
		}
	}
//	cout<<x<<" "<<res<<endl;
	ans = min(max(res,x),ans);
	return res;
}

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		ans = 1e18;
		for(i = 1;i<=n;i++){
			cin>>a[i];
			c[i] = a[i];
		}
		sort(c+1,c+1+n);
		for(i = 1;i<=n;i++){
			cin>>b[i];
		}
		int l = 0,r = n;
		while(l<r){
			int mid = (l+r)/2;
			//res<=mid
		//	cout<<l<<" "<<r<<endl;
			ll t = check(c[mid]);
			check(c[l]);
			check(c[r]);
			if(t<=c[mid])	r = mid;
			else l = mid+1;
		//	cout<<l<<" "<<r<<endl;
		}
		check(l);
		cout<<ans<<endl;

	}

	return 0;
}
/*
2
4
3 7 4 5
2 4 2 4
2
1 2
10 10

*/


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
struct Node{
	ll a,b;
	bool operator < (const Node &A)const {
		return a<A.a;
	}
}c[N];
ll s[N];

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		for(i = 1;i<=n;i++){
			cin>>c[i].a;
		}
		for(i = 1;i<=n;i++){
			cin>>c[i].b;
		}
		sort(c+1,c+1+n);
		s[n+1] = 0;
		for(i = n;i>=1;i--){
			s[i] = s[i+1] + c[i].b;
		//	cout<<i<<" "<<s[i]<<"��׺��"<<endl;
		}
		ll ans = 1e18;
		for(i = 0;i<=n;i++){
		//	cout<<c[i].a<<" "<<s[i+1]<<endl;
			ans = min(ans,max(c[i].a,s[i+1]));
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
ll a[N],b[N],c[N];
ll ans;
//ans<=mid����true;
ll check(ll x){
	ll res = 0;
	int i,j;
	for(i = 1;i<=n;i++){
		if(a[i]<=x){

		}else{
			res += b[i];
		}
	}
//	cout<<x<<" "<<res<<endl;
	ans = min(max(res,x),ans);
	return res;
}

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		ans = 1e18;
		for(i = 1;i<=n;i++){
			cin>>a[i];
			c[i] = a[i];
		}
		sort(c+1,c+1+n);
		for(i = 1;i<=n;i++){
			cin>>b[i];
		}
		int l = 0,r = n;
		while(l<r){
			int mid = (l+r)/2;
			//res<=mid
		//	cout<<l<<" "<<r<<endl;
			ll t = check(c[mid]);
			if(t<=c[mid])	r = mid;
			else l = mid+1;
		//	cout<<l<<" "<<r<<endl;
		}
		check(l);
		cout<<ans<<endl;

	}

	return 0;
}
/*
2
4
3 7 4 5
2 4 2 4
2
1 2
10 10

*/


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
ll a[N],b[N],c[N];
ll ans;
//ans<=mid返回true;
ll check(ll x){
	ll res = 0;
	int i,j;
	for(i = 1;i<=n;i++){
		if(a[i]<=x){

		}else{
			res += b[i];
		}
	}
//	cout<<x<<" "<<res<<endl;
	ans = min(max(res,x),ans);
	return res;
}

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		ans = 1e18;
		for(i = 1;i<=n;i++){
			cin>>a[i];
			c[i] = a[i];
		}
		sort(c+1,c+1+n);
		for(i = 1;i<=n;i++){
			cin>>b[i];
		}
		int l = 0,r = n;
		while(l<r){
			int mid = (l+r)/2;
			//res<=mid
		//	cout<<l<<" "<<r<<endl;
			ll t = check(c[mid]);
			check(c[l]);
			check(c[r]);
			if(t == mid)	break;
			if(t<mid)	r = mid-1;
			else l = mid+1;
		//	cout<<l<<" "<<r<<endl;
		}
		check(l);
		cout<<ans<<endl;

	}

	return 0;
}
/*
2
4
3 7 4 5
2 4 2 4
2
1 2
10 10

*/


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
ll a[N],b[N],c[N];
ll ans;
//ans<=mid����true;
ll check(ll x){
	ll res = 0;
	int i,j;
	for(i = 1;i<=n;i++){
		if(a[i]<=x){

		}else{
			res += b[i];
		}
	}
//	cout<<x<<" "<<res<<endl;
	ans = min(max(res,x),ans);
	return res;
}

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		ans = 1e18;
		for(i = 1;i<=n;i++){
			cin>>a[i];
			c[i] = a[i];
		}
		sort(c+1,c+1+n);
		for(i = 1;i<=n;i++){
			cin>>b[i];
		}
		int l = 0,r = n;
		while(l<r){
			int mid = (l+r)/2;
			//res<=mid
		//	cout<<l<<" "<<r<<endl;
			ll t = check(c[mid]);
		//	cout<<t<<" "<<c[mid]<<endl;
			if(t<=c[mid])	r = mid;
			else l = mid+1;
		//	cout<<l<<" "<<r<<endl;
		}
		check(c[l]);
		cout<<ans<<endl;

	}

	return 0;
}
/*
2
4
3 7 4 5
2 4 2 4
2
1 2
10 10

7
19 66 2 73 74 31 33
4 84 49 1 93 14 43
*/


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;

int n,m;
int a[N],b[N],c[N];
ll ans;
//ans<=mid返回true;
bool check(ll x){
	ll res = 0;
	int i,j;
	for(i = 1;i<=n;i++){
		if(a[i]<=x){

		}else{
			res += b[i];
		}
	}
//	cout<<x<<" "<<res<<endl;
	ans = min(max(res,x),ans);
	if(res<=x)	return true;
	else return false;

}

int main(){
	int i,j;
	int T;
	cin>>T;
	while(T--){
		cin>>n;
		ans = 1e18;
		for(i = 1;i<=n;i++){
			cin>>a[i];
			c[i] = a[i];
		}
		sort(c+1,c+1+n);
		for(i = 1;i<=n;i++){
			cin>>b[i];
		}
		int l = 0,r = n;
		while(l<r){
			int mid = (l+r)/2;
			//ans<=mid
		//	cout<<l<<" "<<r<<endl;
			if(check(c[mid]))	r = mid;
			else l = mid+1;
		//	cout<<l<<" "<<r<<endl;
		}
		check(l);
		cout<<ans<<endl;

	}

	return 0;
}
/*
2
4
3 7 4 5
2 4 2 4
2
1 2
10 10

*/


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    // for(auto x:v)cout<<x.ff<<" ";
    // cout<<endl;
    // for(auto x:v)cout<<x.ss<<" ";
    // cout<<endl;
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=inf;
    // for(lt i=0;i<n;i++)
    // cout<<sum[i]<<" ";
    // cout<<endl;
    for(lt i=0;i<n-1;i++)
    {
        ans=min(ans,max(v[i].ff,sum[i+1]));
    }
    // if(ans==0)ans=v[n-1].ff;
    // lt sm=0;
    // for(lt i=0;i<n;i++)
    // sm+=v[i].ss;
    // ans=min(ans,sm);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i-1]+v[i].ss;
    lt ans=0;
    for(lt i=0;i<n-1;i++)
    {
        if(v[i].ff>=sum[i+1]){
            ans=v[i].ff;break;
        }
    }
    if(ans==0)ans=v[n-1].ff;
    lt sm=0;
    for(lt i=0;i<n;i++)
    sm+=v[i].ss;
    ans=min(ans,sm);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=inf;
    for(lt i=0;i<n-1;i++)
    {
        ans=min(ans,max(v[i].ff,sum[i+1]));
    }
    ans=min(ans,v[n-1].ff);
    lt m=0;
    for(lt i=0;i<n;i++)
    m+=v[i].ss;
    ans=min(ans,m);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    // for(auto x:v)cout<<x.ff<<" ";
    // cout<<endl;
    // for(auto x:v)cout<<x.ss<<" ";
    // cout<<endl;
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=inf;
    // for(lt i=0;i<n;i++)
    // cout<<sum[i]<<" ";
    // cout<<endl;
    for(lt i=0;i<n-1;i++)
    {
        ans=min(ans,max(v[i].ff,sum[i+1]));
    }
    // if(ans==0)ans=v[n-1].ff;
    // lt sm=0;
    // for(lt i=0;i<n;i++)
    // sm+=v[i].ss;
    // ans=min(ans,sm);
    ans=min(ans,v[n-1].ff);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    // for(auto x:v)cout<<x.ff<<" ";
    // cout<<endl;
    // for(auto x:v)cout<<x.ss<<" ";
    // cout<<endl;
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=inf;
    // for(lt i=0;i<n;i++)
    // cout<<sum[i]<<" ";
    // cout<<endl;
    for(lt i=0;i<n-1;i++)
    {
        ans=min(ans,max(v[i].ff,sum[i+1]));
    }
    // if(ans==0)ans=v[n-1].ff;
    // lt sm=0;
    // for(lt i=0;i<n;i++)
    // sm+=v[i].ss;
    // ans=min(ans,sm);
    ans=min(ans,v[n-1].ff);
    lt m=0;
    for(lt i=0;i<n;i++)
    m+=v[i].ss;
    ans=min(ans,m);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    // for(auto x:v)cout<<x.ff<<" ";
    // cout<<endl;
    // for(auto x:v)cout<<x.ss<<" ";
    // cout<<endl;
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=inf;
    // for(lt i=0;i<n;i++)
    // cout<<sum[i]<<" ";
    // cout<<endl;
    for(lt i=0;i<n-1;i++)
    {
        ans=min(ans,max(v[i].ff,sum[i+1]));
    }
    // if(ans==0)ans=v[n-1].ff;
    // lt sm=0;
    // for(lt i=0;i<n;i++)
    // sm+=v[i].ss;
    // ans=min(ans,sm);
    ans=min(ans,v[i].ff);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    // for(auto x:v)cout<<x.ff<<" ";
    // cout<<endl;
    // for(auto x:v)cout<<x.ss<<" ";
    // cout<<endl;
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=inf;
    // for(lt i=0;i<n;i++)
    // cout<<sum[i]<<" ";
    // cout<<endl;
    for(lt i=0;i<n-1;i++)
    {
        ans=min(ans,max(v[i].ff,sum[i+1]));
    }
    // if(ans==0)ans=v[n-1].ff;
    // lt sm=0;
    // for(lt i=0;i<n;i++)
    // sm+=v[i].ss;
    // ans=min(ans,sm);
    ans=min(ans,v[i].ff);
    lt m=0;
    for(lt i=0;i<n;i++)
    m+=v[i].ss;
    ans=min(ans,m);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<math.h>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long int lt;
#define lint long long
#define setbits(x) __builtin_popcountll(x)
#define mem(a, b) memset(a, (b), sizeof(a))
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e17
#define neginf -1e17
#define sp(x, y) fixed << setprecision(x) << y
#define testcase(t) \
    lint t;         \
    cin >> t;       \
    while (t--)
#define power(a, b) (double)pow((double)a, (double)b)
#define FOR(i, n) for (lint i = 0; i < n; i++)
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535
#define pb push_back
#define ss second
#define ff first
#define umap unordered_map
#define ummap unordered_multimap
#define uset unordered_set
#define umset unordered_multiset

//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());//use for shuffling array

typedef unsigned long long ull;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
}

lint me(lint x, lint y, lint p)
{
    lint res = 1;

    x = x % p;

    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

/*-------------------------------SOLUTION---------------------------------*/    ////CODE BY SOMIL JAIN
bool check2(vector<lt>v, lt l, lt sum)
{
    bool knap[v.size()+1][sum+1];
    for (int i = 0; i <= v.size(); i++)
        knap[i][0] = true;

    for (int i = 1; i <= sum; i++)
        knap[0][i] = false;
    for(lt i=1;i<v.size()+1;i++)
    {
        for(lt j=1;j<sum+1;j++)
        {
            if(v[i-1]<=j)knap[i][j]=knap[i-1][j] || knap[i-1][j-v[i-1]];
            else knap[i][j]=knap[i-1][j];
        }
    }
    if(knap[v.size()][sum])return true;
    else return false;
}
bool check33(string s1)
{
    lt n=s1.size();
    set<char>s;
    for(lt i=0;i<n;i++)
    s.insert(s1[i]);
    if( s.size()==1)
    return true;
    return false;
}
// bool cmpr(pair<lt,lt>a,pair<lt,lt>b)
// {
//     if(a.ff!=b.ff)
//     return a.ff>b.ff;
//     return
// }
string kmp_algo(string s)
{
    lt n=s.length();
    lt pi[n];
    pi[0]=0;
    for(lt i=1;i<n;i++)
    {
        lt l=pi[i-1];
        while(l>0 && s[l]!=s[i])
        {
            l=pi[l-1];
        }
        if(s[i]==s[l])
        l++;
        pi[i]=l;
    }
    string temp=s.substr(0,pi[n-1]);
    return temp;
}
lt poww(lt x, lt y,lt p)
{
	lt res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
lt modInverse(lt n, lt p)
{
	return poww(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
lt nCr(lt n, lt r, lt p)
{
	// Base case
	if (r == 0)
		return 1;
		if(r>n)return 0;
	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	lt fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void dfs(vector<vector<lint>> &adj,vector<lint> &v,lint val,vector<lint> &visited)
{
	if(visited[val]==1)
		return;
	visited[val]=1;
	v.pb(val);
	for(auto x:adj[val])
	{
		dfs(adj,v,x,visited);
	}
}
bool func(lt a[], lt n)
{
    set<lt>s;
    for(lt i=0;i<n;i++)
    s.insert(a[i]);
    if(s.size()==1)return true;
    return false;
}
void solve()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lt n;
    cin>>n;
    std::vector<pair<lt,lt>>v(n);
    for(lt i=0;i<n;i++)
    cin>>v[i].ff;
    for(lt i=0;i<n;i++)
    cin>>v[i].ss;
    sort(v.begin(),v.end());
    // for(auto x:v)cout<<x.ff<<" ";
    // cout<<endl;
    // for(auto x:v)cout<<x.ss<<" ";
    // cout<<endl;
    lt sum[n];
    sum[n-1]=v[n-1].ss;
    for(lt i=n-2;i>=0;i--)
    sum[i]=sum[i+1]+v[i].ss;
    lt ans=0;
    // for(lt i=0;i<n;i++)
    // // cout<<sum[i]<<" ";
    // cout<<endl;
    for(lt i=0;i<n-1;i++)
    {
        if(v[i].ff>=sum[i+1]){
            ans=v[i].ff;break;
        }
    }
    if(ans==0)ans=v[n-1].ff;
    lt sm=0;
    for(lt i=0;i<n;i++)
    sm+=v[i].ss;
    ans=min(ans,sm);
    cout<<ans<<endl;
}

int main()
{
    c_p_c();
    lt t;
    cin>>t;
    while(t--){
  solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 17;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (a[i] > x) s += b[i];
	return s <= x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		ll m = l + (r - l) / 2;
		// (l, r]
		if (ok(m)) r = m;
		else l = m;
	}
	return r;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 17;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (x < a[i]) s += b[i];
	return s <= x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		// (l, r]
		ll m = l + (r - l) / 2;
		// (l, l + 2]
		(ok(m) ? r : l) = m;
	}
	return r;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 17;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (a[i] > x) s += b[i];
	return s <= x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		ll m = l + (r - l + 1) / 2;
		// (l, r]
		if (ok(m)) r = m;
		else l = m;
	}
	return r;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 17;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (x <= a[i]) s += b[i];
	return s < x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		ll m = l + (r - l + 1) / 2;
		(ok(m) ? r : l) = m;
	}
	return l;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll solve() {
	int n; cin >> n;
	vector<ll> a(n), b(n);
	for (auto &x : a) cin >> x;
	for (auto &x : b) cin >> x;
	ll A = 0, B = 0;
	for (int i = 0; i < n; ++i) {
		ll opt1 = max({A, a[i], B});
		ll opt2 = max(A, B + b[i]);
		if (opt2 < opt1) B += b[i];
		else ckmax(A, a[i]);
	}
	return max(A, B);
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 17;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (x < a[i]) s += b[i];
	return s <= x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		// (l, r]
		ll m = l + (r - l + 1) / 2;
		// (l, l + 2]
		(ok(m) ? r : l) = m;
	}
	return r;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll solve() {
	int n; cin >> n;
	vector<int> a(n), b(n);
	for (int &x : a) cin >> x;
	for (int &x : b) cin >> x;
	int A = 0, B = 0;
	for (int i = 0; i < n; ++i) {
		int opt1 = max({A, a[i], B});
		int opt2 = max(A, B + b[i]);
		if (opt2 < opt1) B += b[i];
		else ckmax(A, a[i]);
	}
	return max(A, B);
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 17;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (x <= a[i]) s += b[i];
	return s < x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		ll m = l + (r - l) / 2;
		(ok(m) ? r : l) = m;
	}
	return l;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 18;
const ll inf = 1e18;
int a[N], b[N];
int n;

bool ok(ll x) {
	ll s = 0;
	rep(i, 0, n) if (a[i] > x) s += b[i];
	return s <= x;
}

ll solve() {
	cin >> n;
	rep(i, 0, n) cin >> a[i];
	rep(i, 0, n) cin >> b[i];
	ll l = 0, r = inf;
	while (r - l > 1) {
		ll m = l + (r - l + 1) / 2;
		// (l, r]
		if (ok(m)) r = m;
		else l = m;
	}
	return r;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll solve() {
	int n; cin >> n;
	vector<ll> a(n), b(n);
	for (auto &x : a) cin >> x;
	for (auto &x : b) cin >> x;
	ll A = 0, B = 0;
	for (int i = 0; i < n; ++i) {
		int opt1 = max({A, a[i], B});
		int opt2 = max(A, B + b[i]);
		if (opt2 < opt1) B += b[i];
		else ckmax(A, a[i]);
	}
	return max(A, B);
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        for(int i=0;i<n;i++)
            cin>>b[i];

        int *y=max_element(a,a+n);

        int x=*y;

        *y=0;

        int s=0,s1=b[y-a],f=0;

        while(max(s1,*max_element(a,a+n))  <=  x)
        {
            s+=b[y-a];
            y=max_element(a,a+n);
            x=*y;
            *y=0;
            s1+=b[y-a];
            f=1;
        }

        //cout<<s<<" "<<*max_element(a,a+n)<<endl;
        if(f==1)
            cout<<max(s,*max_element(a,a+n))<<endl;
        else
            cout<<x<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    ll int t;
    cin>>t;

    while(t--)
    {
        ll int n;

        cin>>n;

        ll int a[n],b[n];

        for(ll int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<ll int,ll int>> v;

        for(ll int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        ll int k=n-1;

        ll int x=v[k].first;

        ll int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                break;
            }
        }

        if(k==-1 && s1<x)
            cout<<s1<<endl;
        else if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        IOS

        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                break;
            }
        }

        if(k==-1 && s1<x)
            cout<<s1<<endl;
        else if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                //cout<<s1<<" "<<x<<endl;
                if(s1 < x)
                {
                    s=s1;
                    cout<<s<<endl;
                    exit(0);
                }
                break;
            }
        }

        if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        for(int i=0;i<n;i++)
            cin>>b[i];

        int *y=max_element(a,a+n);

        int x=*y;

        *y=0;

        int s=0,s1=b[y-a],f=0;

        while(max(s1,*max_element(a,a+n))  <=  x)
        {
            s+=b[y-a];
            y=max_element(a,a+n);
            x=*y;
            *y=0;
            s1+=b[y-a];
            f=1;
        }

        if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                break;
            }
        }

        if(k==-1)
            cout<<s1<<endl;
        else if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[],int a[], int low, int high)
{
    int pivot = arr[high]; // pivot
    int i = (low - 1); // Index of smaller element

    for (int j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++; // increment index of smaller element
            swap(&arr[i], &arr[j]);
            swap(&a[i], &a[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    swap(&a[i + 1], &a[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[],int a[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
        at right place */
        int pi = partition(arr, a, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr, a, low, pi - 1);
        quickSort(arr, a, pi + 1, high);
    }
}

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        //map <int,int> m;

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
        }

        quickSort(a,b,0,n-1);
        int k=n-1;

        int x=a[k--];

        int s=0,s1=b[k+1],f=0;

        //cout<<s1<<" "<<a[k]<<" "<<k<<endl;
        while(k>=0 && max(s1,a[k])  <=  x)
        {
            s+=b[k+1];
            //cout<<"\t"<<s<<endl;
            x=a[k--];
            //cout<<"\t"<<k<<endl;
            s1+=b[k+1];
            //cout<<"\t"<<s1<<endl;
            f=1;
        }

        if(k==-1 && s1<=x)
            s=s1;

        //cout<<s<<" "<<x<<endl;
        if(f==1 && k!=-1)
            cout<<max(s,x)<<endl;
        else if(f==1 && k==-1)
            cout<<s<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[],int a[], int low, int high)
{
    int pivot = arr[high]; // pivot
    int i = (low - 1); // Index of smaller element

    for (int j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++; // increment index of smaller element
            swap(&arr[i], &arr[j]);
            swap(&a[i], &a[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    swap(&a[i + 1], &a[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[],int a[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
        at right place */
        int pi = partition(arr, a, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr, a, low, pi - 1);
        quickSort(arr, a, pi + 1, high);
    }
}

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
        }

        quickSort(a,b,0,n-1);
        int k=n-1;

        int s=0,s1=b[k],f=0;

        while(k>=1  &&  max(s1,a[k-1]) <= a[k])
        {
            s=s1;

            k--;

            s1+=b[k];

            f=1;
        }

        if(k==0  &&  s1<a[0])
            cout<<s1<<endl;
        else if(f==1)
            cout<<s<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[],int a[], int low, int high)
{
    int pivot = arr[high]; // pivot
    int i = (low - 1); // Index of smaller element

    for (int j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++; // increment index of smaller element
            swap(&arr[i], &arr[j]);
            swap(&a[i], &a[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    swap(&a[i + 1], &a[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[],int a[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
        at right place */
        int pi = partition(arr, a, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr, a, low, pi - 1);
        quickSort(arr, a, pi + 1, high);
    }
}

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
        }

        quickSort(a,b,0,n-1);
        int k=n-1;

        int x=a[k];

        int s=0,s1=b[k],f=0;

        while(k>=1  &&  max(s1,a[k-1]) <= a[k])
        {
            s=s1;

            k--;

            s1+=b[k];

            f=1;
        }

        if(k==0  &&  s1<a[0])
            cout<<s1<<endl;
        else if(f==1)
            cout<<s<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                break;
            }
        }

        if(k==-1)
            cout<<min(s1,x)<<endl;
        else if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n],c[n];

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            c[i]=a[i];
        }

        sort(c,c+n);
        int p=n-1;

        for(int i=0;i<n;i++)
            cin>>b[i];

        int *y=max_element(a,a+n);

        int x=*y;

        *y=0;

        int s=0,s1=b[y-a],f=0;

        while(max(s1,c[p--])  <=  x)
        {
            s+=b[y-a];
            y=max_element(a,a+n);
            x=*y;
            *y=0;
            s1+=b[y-a];
            f=1;
        }

        if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                break;
            }
        }

        if(k==-1 && s1<x)
            cout<<s1<<endl;
        else if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                //cout<<s1<<" "<<x<<endl;
                if(s1 < x)
                {
                    s=s1;
                    cout<<s<<endl;
                    exit;
                }
                break;
            }
        }

        if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        vector <pair<int,int>> v;

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
            v.push_back(make_pair(a[i],b[i]));
        }

        sort(v.begin(),v.end());
        int k=n-1;

        int x=v[k].first;

        int s=0,s1=v[k].second,f=0;

        k--;

        while(max(s1,v[k].first)  <=  x)
        {
            s+=v[k+1].second;
            x=v[k].first;
            s1+=v[k].second;
            f=1;
            k--;
            if(k==-1)
            {
                break;
            }
        }

        if(k==-1 && s1<x)
            cout<<s1<<endl;
        else if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[],int a[], int low, int high)
{
    int pivot = arr[high]; // pivot
    int i = (low - 1); // Index of smaller element

    for (int j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++; // increment index of smaller element
            swap(&arr[i], &arr[j]);
            swap(&a[i], &a[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    swap(&a[i + 1], &a[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[],int a[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
        at right place */
        int pi = partition(arr, a, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr, a, low, pi - 1);
        quickSort(arr, a, pi + 1, high);
    }
}

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        //map <int,int> m;

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }

        for(int i=0;i<n;i++)
        {
            cin>>b[i];
        }

        quickSort(a,b,0,n-1);
        int k=n-1;

        int x=a[k--];

        int s=0,s1=b[k+1],f=0;

        while(max(s1,a[k])  <=  x)
        {
            s+=b[k+1];
            x=a[k--];
            s1+=b[k+1];
            f=1;
        }

        //cout<<s<<" "<<x<<endl;
        if(f==1)
            cout<<max(s,x)<<endl;
        else
            cout<<x<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define  IOS     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    IOS

    int t;
    cin>>t;

    while(t--)
    {
        int n;

        cin>>n;

        int a[n],b[n];

        for(int i=0;i<n;i++)
            cin>>a[i];

        for(int i=0;i<n;i++)
            cin>>b[i];

        int *y=max_element(a,a+n);

        int x=*y;

        *y=0;

        int s=0,s1=b[y-a],f=0;

        while(max(s1,*max_element(a,a+n))  <=  x)
        {
            s+=b[y-a];
            y=max_element(a,a+n);
            x=*y;
            *y=0;
            s1+=b[y-a];
            cout<<b[y-a]<<endl;
            f=1;
        }

        //cout<<s<<" "<<*max_element(a,a+n)<<endl;
        if(f==1)
            cout<<max(s,*max_element(a,a+n))<<endl;
        else
            cout<<x<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;}
const int N = 2e5+5;
typedef long long ll;
using namespace std;
struct node
{
	ll a=0,b=0;
}c[N];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
int sum[N];
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		for(int i=1;i<=n;i++)
		cin>>c[i].a;
		for(int i=1;i<=n;i++)
		cin>>c[i].b;
		sort(c+1,c+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+c[i].b;
		}
		ll ans=sum[n];
		for(int i=1;i<=n;i++)
			ans=min(ans,max(c[i].a,sum[n]-sum[i]));
		cout<<ans<<endl;
	}
	return 0;
}
	//


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;}
const int N = 2e5+5;
typedef long long ll;
using namespace std;
ll a[N];
ll b[N];
int n;
bool check(ll x)
{
	ll sum=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>x)
		sum+=b[i];
	}
	return sum<=x;
}
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)cin>>a[i];
		for(int i=1;i<=n;i++)cin>>b[i];
		ll l=1;
		ll r=0;
		for(int i=1;i<=n;i++)
		r+=b[i];
		while(l<r)
		{
			ll mid=(l+r)/2;
			if(check(mid))r=mid;
			else l=mid+1;
		}
		cout<<r<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int t;
int n;
struct node{
	ll a;
	ll b;
}a[200007];
ll sum[200007];

bool cmp(node xx, node yy)
{
	return xx.a < yy.a;
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i].a);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i].b);
		sort(a + 1, a + 1 + n, cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i] = 0;
			sum[i] = a[i].b + sum[i - 1];
		}
		ll ans = sum[n];
		for(int i=1;i<=n;i++)
		{
			ans = min(ans, max(a[i].a, sum[n] - sum[i]));
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;}
const int N = 2e5+5;
typedef long long ll;
using namespace std;
struct node
{
	int a=0,b=0;
}c[N];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
int sum[N];
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		for(int i=1;i<=n;i++)
		cin>>c[i].a;
		for(int i=1;i<=n;i++)
		cin>>c[i].b;
		sort(c+1,c+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+c[i].b;
		}
		int ans=sum[n];
		for(int i=1;i<=n;i++)
			ans=min(ans,max(c[i].a,sum[n]-sum[i]));
		cout<<ans<<endl;
	}
	return 0;
}
	//


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;}
const int N = 2e5+5;
typedef long long ll;
using namespace std;
struct node
{
	ll a=0,b=0;
}c[N];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
ll sum[N];
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		for(int i=1;i<=n;i++)
		cin>>c[i].a;
		for(int i=1;i<=n;i++)
		cin>>c[i].b;
		sort(c+1,c+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+c[i].b;
		}
		ll ans=sum[n];
		for(int i=1;i<=n;i++)
			ans=min(ans,max(c[i].a,sum[n]-sum[i]));
		cout<<ans<<endl;
	}
	return 0;
}
	//


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;}
const int N = 2e5+5;
typedef long long ll;
using namespace std;
ll a[N];
ll b[N];
int n;
bool check(ll x)
{
	ll sum=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>x)
		sum+=b[i];
	}
	return sum<=x;
}
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)cin>>a[i];
		for(int i=1;i<=n;i++)cin>>b[i];
		int l=1;
		int r=0;
		for(int i=1;i<=n;i++)
		r+=b[i];
		while(l<r)
		{
			ll mid=(l+r)/2;
			if(check(mid))r=mid;
			else l=mid+1;
		}
		cout<<r<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;}
const int N = 2e5+5;
typedef long long ll;
using namespace std;
struct node
{
	int a=0,b=0;
}c[N];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
int sum[N];
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		for(int i=1;i<=n;i++)
		cin>>c[i].a;
		for(int i=1;i<=n;i++)
		cin>>c[i].b;
		sort(c+1,c+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+c[i].b;
		}
		int ans=sum[n];
		for(int i=1;i<=n;i++)
			ans=min(ans,max(c[i].a,sum[n]-sum[i]));
		cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<ll, 2> > v(n);

    vector<ll> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {a[i], b[i]};
    }

    sort(v.begin(), v.end());

    vector<ll> sufSum(n+1);
    sufSum[n-1] = v[n-1][1];
    for (int i=n-2; i>=0; i--) {
        sufSum[i] =sufSum[i+1] + v[i][1];
    }
    v.push_back({0, 0});
    ll ans = sufSum[0];
    for (int i=0; i<n; i++) {
        ans = min (ans, max(v[i][0], sufSum[i+1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<int, 2> > v(n);

    vector<int> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {b[i], a[i]};
    }

    sort(v.begin(), v.end(), [](const array<int,2>& x, const array<int,2> &y)->bool{
        int deltaX = x[1] - x[0];
        int deltaY = y[1] - y[0];
        if (deltaX > deltaY)
            return x > y;
        return x < y;
    });

    vector<int> sufDelMax(n+1);
    sufDelMax[n-1] = v[n-1][1];
    for (int i=n-2; i>=0; i--) {
        sufDelMax[i] = max(sufDelMax[i+1], v[i][1]);
    }

    int ans = sufDelMax[0];
    int pickUpTime = 0;
    for (int i=0; i<n; i++) {
        pickUpTime += v[i][0];
        ans = min (ans, max(pickUpTime, sufDelMax[i+1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<int, 2> > v(n);

    vector<int> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {b[i], a[i]};
    }

    sort(v.begin(), v.end(), [](const array<int,2>& x, const array<int,2> &y)->bool{
        int deltaX = x[1] - x[0];
        int deltaY = y[1] - y[0];
        return deltaX >= deltaY;
    });

    vector<int> sufDelMax(n+1);
    sufDelMax[n-1] = v[n-1][1];
    for (int i=n-2; i>=0; i--) {
        sufDelMax[i] = max(sufDelMax[i+1], v[i][1]);
    }

    int ans = sufDelMax[0];
    int pickUpTime = 0;
    for (int i=0; i<n; i++) {
        pickUpTime += v[i][0];
        ans = min (ans, max(pickUpTime, sufDelMax[i+1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<ll, 2> > v(n);

    vector<ll> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {b[i], a[i]};
    }

    sort(v.begin(), v.end(), [](const array<ll,2>& x, const array<ll,2> &y)->bool{
        if (x[1] == y[1])
            return x[0] < y[0];
        return x[1] > y[1];
    });

    // vector<int> sufDelMax(n+1);
    // sufDelMax[n-1] = v[n-1][1];
    // for (int i=n-2; i>=0; i--) {
    //     sufDelMax[i] = max(sufDelMax[i+1], v[i][1]);
    // }
    v.push_back({0, 0});
    ll ans = v[0][1];
    ll pickUpTime = 0;
    for (int i=0; i<n; i++) {
        pickUpTime += v[i][0];
        ans = min (ans, max(pickUpTime, v[i+1][1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<int, 2> > v(n);

    vector<int> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {a[i], b[i]};
    }

    sort(v.begin(), v.end());

    vector<int> sufSum(n+1);
    sufSum[n-1] = v[n-1][1];
    for (int i=n-2; i>=0; i--) {
        sufSum[i] =sufSum[i+1] + v[i][1];
    }
    v.push_back({0, 0});
    int ans = sufSum[0];
    for (int i=0; i<n; i++) {
        ans = min (ans, max(v[i][0], sufSum[i+1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int solve(int index) {

}

void testcase() {
    int n; cin >> n;
    vector<array<int, 2> > v(n);

    vector<int> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        v[i] = {b[i], a[i]};
    }

    sort(v.begin(), v.end(), [](const array<int,2>& x, const array<int,2> &y)->bool{
        if (x[0] != y[0])
            return x[0] < y[0];
        return x[1] > y[1];
    });

    vector<int> sufDelMax(n+1);
    sufDelMax[n-1] = v[n-1][1];
    for (int i=n-2; i>=0; i--) {
        sufDelMax[i] = max(sufDelMax[i+1], v[i][1]);
    }

    int ans = sufDelMax[0];
    int pickUpTime = 0;
    for (int i=0; i<n; i++) {
        pickUpTime += v[i][0];
        ans = min (ans, max(pickUpTime, sufDelMax[i+1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<int, 2> > v(n);

    vector<int> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {b[i], a[i]};
    }

    sort(v.begin(), v.end(), [](const array<int,2>& x, const array<int,2> &y)->bool{
        if (x[1] == y[1])
            return x[0] < y[0];
        return x[1] > y[1];
    });

    // vector<int> sufDelMax(n+1);
    // sufDelMax[n-1] = v[n-1][1];
    // for (int i=n-2; i>=0; i--) {
    //     sufDelMax[i] = max(sufDelMax[i+1], v[i][1]);
    // }
    v.push_back({0, 0});
    int ans = v[0][1];
    int pickUpTime = 0;
    for (int i=0; i<n; i++) {
        pickUpTime += v[i][0];
        ans = min (ans, max(pickUpTime, v[i+1][1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 1

using namespace std;
using ll = long long;

const int N = 2e5;
int del[N], pickup[N];
int n;
void testcase() {
    cin >> n;
    vector<array<int, 2> > v(n);

    vector<int> a(n), b(n);
    for (auto &it : a) cin >> it;
    for (auto &it : b) cin >> it;

    for (int i=0; i<n; i++) {
        del[i] = a[i];
        pickup[i] = b[i];
        v[i] = {b[i], a[i]};
    }

    sort(v.begin(), v.end(), [](const array<int,2>& x, const array<int,2> &y)->bool{
        if (x[1] == y[1])
            return x[0] < y[0];
        return x[1] > y[1];
        // int deltaX  = x[1] - x[0];
        // int deltaY = y[1] - y[0];
        // return deltaX >= deltaY;
    });

    vector<int> sufDelMax(n+1);
    sufDelMax[n-1] = v[n-1][1];
    for (int i=n-2; i>=0; i--) {
        sufDelMax[i] = max(sufDelMax[i+1], v[i][1]);
    }

    int ans = sufDelMax[0];
    int pickUpTime = 0;
    for (int i=0; i<n; i++) {
        pickUpTime += v[i][0];
        ans = min (ans, max(pickUpTime, sufDelMax[i+1]));
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[200006], b[200006];
signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
            cin >> b[i];
        int l = 1, r = 1e10;
        while(l < r)
        {
            int mid = l + r >> 1;
            int sum = 0;
            for(int i = 1; i <= n; i++)
            {
                if(a[i] > mid)
                {
                    sum += b[i];
                }
            }
            if(sum <= mid) r = mid;
            else l = mid+1;
        }
        cout << r << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[200006], b[200006];
signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
            cin >> b[i];
        int l = 1, r = 1e10;
        while(l < r)
        {
            int mid = l + r >> 1;
            int sum = 0;
            for(int i = 1; i <= n; i++)
            {
                if(a[i] > mid)
                {
                    sum += b[i];
                }
            }
            if(sum <= mid) r = mid-1;
            else l = mid+1;
        }
        cout << r << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[200006], b[200006];
unsigned main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
            cin >> b[i];
        int l = 1, r = 1e10;
        while(l < r)
        {
            int mid = l + r >> 1;
            int sum = 0;
            for(int i = 1; i <= n; i++)
            {
                if(a[i] > mid)
                {
                    sum += b[i];
                }
            }
            if(sum <= mid) r = mid-1;
            else l = mid+1;
        }
        cout << r << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[200006], b[200006];
signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
            cin >> b[i];
        int l = 1, r = 1e10;
        int ans;
        while(l <= r)
        {
            int mid = (l+r) >> 1;
            int sum = 0;
            for(int i = 1; i <= n; i++)
            {
                if(a[i] > mid)
                {
                    sum += b[i];
                }
            }
            if(sum < mid) {ans = mid;r = mid-1;}
            else l = mid+1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Node{
    int x,y;
}node[200006];

bool cmp(const Node &a,Node &b){
    return a.x < b.x;
}

int sum[200006];
int main()
{
    int t,a,b,n,m;
    ios::sync_with_stdio(false);
    cin>>t;
    while (t--)
    {
        cin>>n;
        for(int i = 0;i<n;i++){
            cin>>node[i].x;
        }
        for(int i = 0;i<n;i++){
            cin>>node[i].y;
        }
        sort(node,node+n,cmp);
        sum[0] = node[0].y;
        for(int i = 1;i<n;i++){
            sum[i] = sum[i-1] + node[i].y;
        }
        int ans = 0x7fffffff;
        for(int i = 0;i<n;i++) {
            ans = min(ans, max(node[i].x,sum[n-1]-sum[i]));
            if(i == n-1) ans = min(sum[n-1],node[i-1].x);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int c[100001]={0};
int main()
{
    bool ok=true;
    int n,i,mx_so_far=0,mx_h=0;
    memset(c,-1,sizeof(c));
    cin>>n;
    int a[n];
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    if(i>1)
        if(a[i-1]+a[i-2]==a[i])
            c[i]=1;
    }
    for(i=2;i<n;i++)
    {
        mx_h += c[i];
        if(mx_h<0)
            {
            mx_h=0;
            ok=false;
            }
        mx_so_far=max(mx_so_far,mx_h);
    }
    cout<<(n==1?1:mx_so_far+2)<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int c[10001]={0};
int main()
{
    bool thr=true,fou=true;
    int n,i,mx_so_far=0,mx_h=0;
    memset(c,-1,sizeof(c));
    cin>>n;
    int a[n];
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    if(i>1)
        if(a[i-1]+a[i-2]==a[i])
            c[i]=1;
    }
    for(i=2;i<n;i++)
    {
        mx_h += c[i];
        if(mx_h<0)
            {
            mx_h=0;
            thr=0;
            if(i>=3)
                fou=false;
            }
        mx_so_far=max(mx_so_far,mx_h);
    }
    cout<<max(mx_so_far + thr + fou,n==1?1:2)<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int c[100001]={0};
int main()
{
    bool ok=true;
    int n,i,mxs=0,mx;
    memset(c,-1,sizeof(c));
    cin>>n;
    mx=(n==1?1:2);
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    c[0]=1;
    c[1]=2;
    for(i=2;i<n;i++)
    {
        if(a[i]==a[i-1]+a[i-2])
            {
            c[i]=c[i-1]+1;
            mx=max(mx,c[i]);
            }
        else
            c[i]=2;
    }
    cout<<mx<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int c[10001]={0};
int main()
{
    bool thr=true,fou=true;
    int n,i,mx_so_far=0,mx_h=0;
    memset(c,-1,sizeof(c));
    cin>>n;
    int a[n];
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    if(i>1)
        if(a[i-1]+a[i-2]==a[i])
            c[i]=1;
    }
    for(i=2;i<n;i++)
    {
        mx_h += c[i];
        if(mx_h<0)
            {
            mx_h=0;
            thr=0;
            if(i>=3)
                fou=false;
            }
        mx_so_far=max(mx_so_far,mx_h);
    }
    cout<<(n==1?1:max(mx_so_far + thr + fou,2))<<endl;
    return 0;

}