#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        if (x == 0) {
            if (y % 2 != k % 2) {
                cout << -1 << endl;
                continue;
            }
        }
        ll lk = k - x;
        ll ly = y - x;
        if (x == y) {
            if (lk % 2) {
                cout << k - 2 << endl;
            } else {
                cout << k << endl;
            }
        } else {
            if (lk % 2 == ly % 2) {
                cout << k << endl;
            } else {
                if (ly % 2) {
                    cout << k - 1 << endl;
                } else {
                    cout << k - 2 << endl;
                }
            }
        }
    }

    return 0;
}

/*
ll get_more_

ll get_sum (ll N) {
    vector < ll > mul;
    while (N > 0) {
        ll nt = N % 1000; N /= 1000;

    }
}

int main () {

    ll T;
    cin >> T;

    REP(kai, T) {
        ll L,R;
        cin >> L >> R;
        cout << get_sum(R) - get_sum(L) << endl;
    }

    return 0;
}
*/


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll lk = k - x;
        ll ly = y - x;
        if (x == y) {
            if (lk % 2) {
                cout << k - 2 << endl;
            } else {
                cout << k << endl;
            }
        } else {
            if (lk % 2 == ly % 2) {
                cout << k << endl;
            } else {
                if (ly % 2) {
                    cout << k - 1 << endl;
                } else {
                    cout << k - 2 << endl;
                }
            }
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll left_move = k - y;
        if (left_move % 2) {
            if ((y - x) % 2 == 1) {
                cout << y + left_move - left_move % 2 << endl;
            } else {
                cout << y - 1 << endl;
            }
        } else {
            if ((y - x) % 2 == 1) {
                cout << y + left_move - left_move % 2 << endl;
            } else {
                cout << y + left_move << endl;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        if (y % 2 == k % 2) {
            if ((y - x) % 2 == 1) {
                cout << k - 1 << endl;
            } else {
                cout << k << endl;
            }
        } else {
            if ((y - x) % 2 == k % 2) {
                cout << k - 1 << endl;
            } else {
                cout << k - 2 << endl;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll lk = k - y;
        ll ly = y - x;
        if (lk == 1) {
            if (ly % 2 == 1) {
                cout << k - 1 << endl;
            } else {
                cout << k - 2 << endl;
            }
        } else if (lk % 2 == 0) {
            if (ly % 2 == 1) {
                cout << k - 1 << endl;
            } else {
                cout << k << endl;
            }
        } else {
            if (ly % 2 == 1) {
                cout << k - 1 << endl;
            } else {
                cout << k - 2 << endl;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll left_move = k - y;
        if (left_move == 1) {
            cout << y - 1 << endl;
        } else {
            cout << y + left_move - left_move % 2 << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        if (y % 2 == k % 2) {
            cout << k << endl;
        } else {
            if ((y - x) % 2 == k % 2) {
                cout << k - 1 << endl;
            } else {
                cout << k - 2 << endl;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        ll left_move = k - y;
        ll y_move = y - x;
        if (left_move == 1) {
            cout << x + y_move - (y_move % 2) - 1 << endl;
        } else {
            cout << x + y_move - (y_move % 2) + left_move << endl;
        }
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;

int main () {

    ll q;
    cin >> q;

    REP(kai,q) {
        ll x,y,k;
        cin >> x >> y >> k;
        x = abs(x);
        y = abs(y);
        if (x > y) swap(x, y);
        if (y > k) {
            cout << -1 << endl;
            continue;
        }
        if (x == 0) {
            if (y + 1 == k) {
                cout << -1 << endl;
                continue;
            }
        }
        ll lk = k - x;
        ll ly = y - x;
        if (x == y) {
            if (lk % 2) {
                cout << k - 2 << endl;
            } else {
                cout << k << endl;
            }
        } else {
            if (lk % 2 == ly % 2) {
                cout << k << endl;
            } else {
                if (ly % 2) {
                    cout << k - 1 << endl;
                } else {
                    cout << k - 2 << endl;
                }
            }
        }
    }

    return 0;
}

/*
ll get_more_

ll get_sum (ll N) {
    vector < ll > mul;
    while (N > 0) {
        ll nt = N % 1000; N /= 1000;

    }
}

int main () {

    ll T;
    cin >> T;

    REP(kai, T) {
        ll L,R;
        cin >> L >> R;
        cout << get_sum(R) - get_sum(L) << endl;
    }

    return 0;
}
*/


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( x > k || y > k){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				k-=y;
				ll l = x-y;
				k-=l;
				ans+=k;
				if(l % 2 ==0){
					if(k%2 == 0)
						ans-=2;
				}
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( k<mn){
			cout<<-1<<endl;
			continue;
		}
		else{
			ll ans = y;
			k-=y;
			ll l = x-y;
			k-=l;
			ans+=k;
			if(l % 2 ==0){
				if(k%2 == 0)
					ans-=2;
			}
			cout<<ans<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( x > k || y > k){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				ll l = k - ans;
				ans+=l;
				if((x-y)%2!=0)
					ans--;
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( x > k || y > k){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y + (x-y);
				ll l = k - ans;
				ans+=l;
				if(l%2 != 0)
					ans--;
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( k < y){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				k-=y;
				ll l = x-y;
				k-=l;
				ans+=k;
				if(l % 2 ==0){
					if(k%2 == 0)
						ans-=2;
				}
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( k < x){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				// cout<<ans<<endl;
				k-=y;
				ll l = x-y;
				ans+=(l/2)*2;
				// cout<<ans<<endl;
				k-=l;
				ans+=k;
				// cout<<ans<<endl;
				if(l % 2 ==0){
					if(k%2)
						ans-=2;
				}
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( x > k || y > k){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				k-=y;
				ll l = x-y;
				ans+=l;
				if(l==1 && k==1)
					ans--;
				k-=l;
				ans+=k;
				if(k%2!=0)
					ans--;
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( x > k || y > k){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				k-=y;
				ll l = x-y;
				ans+=l;
				k-=l;
				// cout<<k<<endl;
				ans+=k;
				if(k%2!=0)
					ans--;
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( k< y){
			cout<<-1<<endl;
			continue;
		}
		else{
			ll ans = y;
			k-=y;
			ll l = x-y;
			k-=l;
			ans+=k;
			if(l % 2 ==0){
				if(k%2 == 0)
					ans-=2;
			}
			cout<<ans<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main(){
	// fast();
	ll n;
	cin>>n;
	while(n--){
		ll x,y,k;
		cin>>x>>y>>k;
		x = abs(x);
		y = abs(y);
		if(x<y)
			swap(x,y);
		if( x > k || y > k){
			cout<<-1<<endl;
			continue;
		}
		else{

			if(x==y){
				ll ans = k;
				if((k-y)%2 !=0)
					ans-=2;
				cout<<ans<<endl;
			}
			else{
				ll ans = y;
				ll l = k - ans;
				ans+=l;
				// if(l%2 != 0)
					ans--;
				cout<<ans<<endl;
			}

		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
ll diagonales=k-k%2;
if(n!=m)
cout<<diagonales<<'\n';
else{
   if(k==n)cout<<n<<'\n';
   else{
    cout<<k-2<<'\n';
   }
}
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
ll diagonales=min(n,m)+k-max(n,m);
if(n!=m)
cout<<diagonales<<'\n';
else{
   if(k==n)cout<<n<<'\n';
   else{
    cout<<k-2<<'\n';
   }
}
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
ll diagonales=k-1;
if(n!=m)
cout<<diagonales<<'\n';
else{
   if(k==n)cout<<n<<'\n';
   else{
    cout<<k-2<<'\n';
   }
}
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
   if(k==n)cout<<n<<'\n';
   else if(n==m){
    cout<<k-2<<'\n';
   }
else{
cout<<k-abs(n-m)%2<<'\n';
}
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
   if(k==n)cout<<n<<'\n';
   else if(n==m){
    cout<<k-2<<'\n';
   }
else{
cout<<(k-(ll)abs(n-m)%2-(k%2==0))<<'\n';
}
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
cout<<(k-(((abs(n-m)%2)==0)?(((k-max(n,m))%2)*2):(((k-max(n,m))%2))))<<'\n';
}
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>q;
while(q--){
ll n,m,k;
cin>>n>>m>>k;
ll minis=max(n,m);
if(k<minis){
    cout<<-1<<'\n';
    continue;
}
cout<<(k-(((abs(n-m)%2)==0)?(((k-max(n,m))%2)*2):(1)))<<'\n';
}
return 0;}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        if(cur>k)cout<<-1<<endl;
        else if(n==m&&(k-m)%2)
        {
            cout<<k-2<<endl;
        }
        else if(k-ans>cur-ans)
        {
            cout<<k-k%2<<endl;
        }
        else
        {
            cout<<k-k%2<<endl;
        }
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        cur-=ans;
        k-=ans;
        if(k<cur)cout<<-1<<endl;
        else if((!cur)&&k%2)cout<<k-2+ans<<endl;
        else if(cur%2&&k%2)cout<<k-1+ans<<endl;
        else if((!(cur%2))&&(!(k%2)))cout<<k+ans<<endl;
        else if((cur%2)&&(!(k%2)))cout<<k-1+ans<<endl;
        else if((!(cur%2))&&(k%2))cout<<k-1+ans<<endl;
        // ans+=(cur-ans)-((cur-ans)%2);
        // ans+=(k-cur)/2;
        /*if(cur>k)cout<<-1<<endl;
        else if(n==m)
        {
            if((k-m)%2)cout<<k-2<<endl;
            else cout<<k<<endl;
        }
        else// if(k-ans>cur-ans)
        {
            if((cur-ans)%2==0)cout<<k-(k-cur)%2<<endl;
            else cout<<k-1<<endl;
        }*/
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        if(cur>k)cout<<-1<<endl;
        else if(n==m)
        {
            if((k-m)%2)cout<<k-2<<endl;
            else cout<<k<<endl;
        }
        else// if(k-ans>cur-ans)
        {
            if((cur-ans)%2==0)cout<<k-(k-cur)%2<<endl;
            else cout<<k-1<<endl;
        }
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        cur-=ans;
        k-=ans;
        if(k<cur)cout<<-1<<endl;
        else if((!cur)&&k%2)cout<<k-2+ans<<endl;
        else if(cur%2&&k%2)cout<<k-1+ans<<endl;
        else if((!cur%2)&&(!k%2))cout<<k+ans<<endl;
        else if((cur%2)&&(!k%2))cout<<k-1+ans<<endl;
        else if((!cur%2)&&(k%2))cout<<k-1+ans<<endl;
        // ans+=(cur-ans)-((cur-ans)%2);
        // ans+=(k-cur)/2;
        /*if(cur>k)cout<<-1<<endl;
        else if(n==m)
        {
            if((k-m)%2)cout<<k-2<<endl;
            else cout<<k<<endl;
        }
        else// if(k-ans>cur-ans)
        {
            if((cur-ans)%2==0)cout<<k-(k-cur)%2<<endl;
            else cout<<k-1<<endl;
        }*/
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        if(cur>k)cout<<-1<<endl;
        else if(n==m)
        {
            if((k-m)%2)cout<<k-2<<endl;
            else cout<<k<<endl;
        }
        else if(k-ans>cur-ans)
        {
            cout<<k-(k-cur)%2<<endl;
        }
        else
        {
            cout<<k-(cur-k)%2<<endl;
        }
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        if(cur>k)cout<<-1<<endl;
        else if(n==m)
        {
            if((k-m)%2)cout<<k-2<<endl;
            else cout<<k<<endl;
        }
        else if(k-ans>cur-ans)
        {
            cout<<k-k%2<<endl;
        }
        else
        {
            cout<<k-k%2<<endl;
        }
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll q,n,m,k;
    cin>>q;
    while(q--)
    {
        cin>>n>>m>>k;
        ll ans,cur;
        ans=min(n,m);
        cur=max(n,m);
        cur-=ans;
        k-=ans;
        if(k<cur)cout<<-1<<endl;
        else if((!cur)&&k%2)cout<<k-2+ans<<endl;
        else if(cur%2&&k%2)cout<<k-1+ans<<endl;
        else if((!(cur%2))&&(!(k%2)))cout<<k+ans<<endl;
        else if((cur%2)&&(!(k%2)))cout<<k-1+ans<<endl;
        else if((!(cur%2))&&(k%2))cout<<k-2+ans<<endl;
        // ans+=(cur-ans)-((cur-ans)%2);
        // ans+=(k-cur)/2;
        /*if(cur>k)cout<<-1<<endl;
        else if(n==m)
        {
            if((k-m)%2)cout<<k-2<<endl;
            else cout<<k<<endl;
        }
        else// if(k-ans>cur-ans)
        {
            if((cur-ans)%2==0)cout<<k-(k-cur)%2<<endl;
            else cout<<k-1<<endl;
        }*/
    }
	return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli n,i,j=0,k=0,c=0,q=1,l,r,x,m;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1";
            else{
                if(n==m){
                    cout<<k-2*((k-m)%2)<<endl;
                }
                else{
                c+=min(n,m);
                k-=c;
                c+=k-abs(n-m);
                cout<<c<<endl;
                }
            }
        }
     // }
    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli n,i,j=0,k=0,c=0,q=1,l,r,x,m,c1,k1;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1";
            else{
                k1=k;
                if(n==m){
                   c1=k1-2*((k1-m)%2);
                }
                else{
                c1+=min(n,m)+abs(n-m);
                k1-=c1;
                if(k1%2){
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1-2;
                }
                else{
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1;
                }
                }
                if(n==m){
                    c=k-2*((k-m)%2);
                }
                else{
                c+=min(n,m);
                k-=c;
                if(k%2){
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k-2;
                }
                else{
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k;
                }
                }
                cout<<min(c,c1)+abs(c-c1)<<endl;
            }
        }
     // }
    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli n,i,j=0,k=0,c=0,q=1,l,r,x,m,c1,k1;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1\n";
            else{
                k1=k;
                if(n==m){
                   c1=k1-2*((k1-m)%2);
                }
                else{
                c1+=min(n,m)+abs(n-m);
                k1-=c1;
                if(k1%2){
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1-2;
                }
                else{
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1;
                }
                }
                if(n==m){
                    c=k-2*((k-m)%2);
                }
                else{
                c+=min(n,m);
                k-=c;
                if(k%2){
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k-2;
                }
                else{
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k;
                }
                }
                cout<<min(c,c1)+abs(c-c1)<<endl;
            }
        }
     // }
    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli n,i,j=0,k=0,c=0,q=1,l,r,x,m,c1=0,k1=0;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1";
            else{
                k=k1;
                if(n==m){
                   c1=k1-2*((k-m)%2);
                }
                else{
                c1+=min(n,m)+abs(m-n);
                k1-=c1;
                if(k1%2){
                    if((n-m)%2)
                    c1+=k-1;
                    else
                    c1+=k-2;
                }
                else{
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1;
                }
                }
                if(n==m){
                   c=k-2*((k-m)%2);
                }
                else{
                c+=min(n,m);
                k-=c;
                if(k%2){
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k-2;
                }
                else{
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k;
                }

                }
                if(c<c1)
                cout<<c1<<endl;
                else
                cout<<c<<endl;
            }
        }
     // }
    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli n,i,j=0,k=0,c=0,q=1,l,r,x,m;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1";
            else{
                if(n==m){
                    cout<<k<<endl;
                }
                else{
                c+=min(n,m);
                k-=c;
                if(k%2){
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k-2;
                }
                else{
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k;
                }

                cout<<c<<endl;
                }
            }
        }
     // }
    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli q;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            lli n,i,j=0,k=0,c=0,l,r,x,m,c1,k1;
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1\n";
            else{
                k1=k;
                if(n==m){
                   c1=k1-2*((k1-m)%2);
                }
                else{
                c1+=min(n,m)+abs(n-m);
                k1-=c1;
                if(k1%2){
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1-2;
                }
                else{
                    if((n-m)%2)
                    c1+=k1-1;
                    else
                    c1+=k1;
                }
                }
                if(n==m){
                    c=k-2*((k-m)%2);
                }
                else{
                c+=min(n,m);
                k-=c;
                if(k%2){
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k-2;
                }
                else{
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k;
                }
                }
                cout<<min(c,c1)+abs(c-c1)<<endl;
            }
        }
     // }
    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mp for(auto it : m)
using namespace std;
int main()
{
    IO
    int t;

   // cin>>t;
    //while(t--){
        lli n,i,j=0,k=0,c=0,q=1,l,r,x,m;
        cin>>q;
         // string s,w;
         // cin>>s;
        /* lli a[n],b[n],e[1001];
         //map<lli,lli> m;
        for(i=0;i<n;i++){
            cin>>a[i];
         }*/
        while(q--){
            cin>>n>>m>>k;
            if(k<min(n,m)+abs(n-m))
            cout<<"-1";
            else{
                if(n==m){
                    cout<<k-2*((k-m)%2)<<endl;
                }
                else{
                c+=min(n,m);
                k-=c;
                if(k%2){
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k-2;
                }
                else{
                    if((n-m)%2)
                    c+=k-1;
                    else
                    c+=k;
                }

                cout<<c<<endl;
                }
            }
        }
     // }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            //long long a=k-x;
            //if(a%2==0)cout<<x+a<<endl;
            //else cout<<
            if(x==1){
                if(k%2==0)cout<<"0"<<endl;
                else cout<<"1"<<endl;
                continue;
            }
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
            continue;
        }
        else{
            long long a;
            a=min(x,y);
            if(k-a-abs(x-y)==0)cout<<a<<endl;
            else{
                if(k-a-abs(x-y)>=2*abs(x-y)){
                    if((k-a)%2==0){
                    if(abs(x-y)%2==0)cout<<k<<endl;
                    else cout<<k-1<<endl;
                    }
                    else{
                        if(abs(x-y)%2==0)cout<<k-1<<endl;
                        else cout<<k<<endl;
                    }
                }
                else{
                    long long b=k-a-abs(x-y);
                    cout<<b+a<<endl;
                }
            }

        }
    }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
        }
        else{
            long long b=abs(x-y);
            if(b%2==0){if((k-min(x,y))%2==0)cout<<k<<endl;else cout<<k-2<<endl;}
            else {cout<<k-1<<endl;}
        }
    }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            //long long a=k-x;
            //if(a%2==0)cout<<x+a<<endl;
            //else cout<<
            if(x==1){
                if(k%2==0)cout<<"0"<<endl;
                else cout<<"1"<<endl;
                continue;
            }
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
        }
        else{
            long long a;
            a=min(x,y);
            if(k-a-abs(x-y)==0)cout<<a<<endl;
            else{
                if(k-a-abs(x-y)>=2*abs(x-y)){
                    long long b=k-a-abs(x-y);
                    cout<<a+b<<endl;
                }
                else{
                    long long b=k-a-abs(x-y);
                    cout<<b+a<<endl;
                }
            }

        }
    }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
        }
        else{
            long long b=abs(x-y);
            if(b%2==0){if((k-min(x,y))%2==0)cout<<k<<endl;else cout<<k-1<<endl;}
            else {if((k-min(x,y))%2==0)cout<<k-1<<endl;else cout<<k<<endl;}
        }
    }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            if(x==1){
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
                continue;
            }
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
        }
        else{
            long long a=min(x,y), b=abs(x-y);
            if(b%2==0)cout<<k<<endl;
            else cout<<k-1<<endl;
        }
    }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            if(x==1){
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
                continue;
            }
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
        }
        else{
            long long a=min(x,y), b=k-a-abs(x-y);
            cout<<a+b<<endl;
        }
    }
    return 0;
}


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

bool comp(int a, int b){
    return a>b;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long x,y,k;
        cin>>x>>y>>k;
        if(k<max(x,y)){cout<<"-1"<<endl;continue;}
        if(x==y){
            if(x%2==0){
                if(k%2==0)cout<<k<<endl;
                else cout<<k-2<<endl;
            }
            else{
                if(k%2==0)cout<<k-2<<endl;
                else cout<<k<<endl;
            }
        }
        else{
            long long b=abs(x-y);
            if(b%2==0){if((k-min(x,y))%2==0)cout<<k<<endl;else cout<<k-2<<endl;}
            else {if((k-min(x,y))%2==0)cout<<k-1<<endl;else cout<<k<<endl;}
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define  MEM(a,x)   memset(a,x,sizeof(a))
#define  pi         acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  vpl    vector<pll>
#define  MX     100005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
int main()
{
   //freopen ("output.txt","w",stdout);
   //freopen ("input.txt","r",stdin);
    FIO;
    ll q;
    cin>>q;
    while(q--)
    {
        ll n,m,k1,k;
        cin>>n>>m>>k1;
        k=k1;
        if((n+m)%2==0)
        {
            if(k1<max(n,m))
            {
                cout<<-1<<endl;
                continue;
            }
            if(n%2==0)
            {
                if(k%2==0)
                {
                    cout<<k<<endl;
                    continue;
                }
            }
            else if(n%2!=0)
            {
                if(k%2!=0)
                {
                    cout<<k<<endl;
                    continue;
                }
            }
            k-=((max(n,m)-1)+2);
            if(k<0)
            {
                if(k1==max(n,m))
                {
                    cout<<k1<<endl;
                    continue;
                }
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
        else
        {
            k-=((max(n,m)-1)+1);
            if(k<0)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define  MEM(a,x)   memset(a,x,sizeof(a))
#define  pi         acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  vpl    vector<pll>
#define  MX     100005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
int main()
{
   //freopen ("output.txt","w",stdout);
   //freopen ("input.txt","r",stdin);
    FIO;
    ll q;
    cin>>q;
    while(q--)
    {
        ll n,m,k;
        cin>>n>>m>>k;
        if(k<max(n,m))
        {
            cout<<-1<<endl;
            continue;
        }
        if((n+m)%2==0)
        {
            if(k%2==0)
            {
                cout<<k<<endl;
                continue;
            }
            else
            {
                if((max(n,m)-1)+2>k)
                {
                    cout<<-1<<endl;
                    continue;
                }
                cout<<max(n,m)-1+(k-2-(max(n,m)-1))<<endl;
            }
        }
        else
        {
            if(max(n,m)>k)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<max(n,m)-1+(k-1-(max(n,m)-1))<<endl;

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define  MEM(a,x)   memset(a,x,sizeof(a))
#define  pi         acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  vpl    vector<pll>
#define  MX     100005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
int main()
{
   //freopen ("output.txt","w",stdout);
   //freopen ("input.txt","r",stdin);
    FIO;
    ll q;
    cin>>q;
    while(q--)
    {
        ll n,m,k;
        cin>>n>>m>>k;
        if((n+m)%2==0)
        {
            k-=((max(n,m)-1)+2);
            if(k<0)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
        else
        {
            k-=((max(n,m)-1)+1);
            if(k<0)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define  MEM(a,x)   memset(a,x,sizeof(a))
#define  pi         acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  vpl    vector<pll>
#define  MX     100005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
int main()
{
   //freopen ("output.txt","w",stdout);
   //freopen ("input.txt","r",stdin);
    FIO;
    ll q;
    cin>>q;
    while(q--)
    {
        ll n,m,k1,k;
        cin>>n>>m>>k1;
        k=k1;
        if((n+m)%2==0)
        {
            k-=((max(n,m)-1)+2);
            if(k<0)
            {
                if(k1==max(n,m))
                {
                    cout<<k1<<endl;
                    continue;
                }
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
        else
        {
            k-=((max(n,m)-1)+1);
            if(k<0)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define  MEM(a,x)   memset(a,x,sizeof(a))
#define  pi         acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  vpl    vector<pll>
#define  MX     100005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
int main()
{
   //freopen ("output.txt","w",stdout);
   //freopen ("input.txt","r",stdin);
    FIO;
    ll q;
    cin>>q;
    while(q--)
    {
        ll n,m,k1,k;
        cin>>n>>m>>k1;
        k=k1;
        if((n+m)%2==0)
        {
            if(k1%2==0)
            {
                if(k1<(n+m))
                {
                    cout<<-1<<endl;
                    continue;
                }
                cout<<k1<<endl;
                continue;
            }
            k-=((max(n,m)-1)+2);
            if(k<0)
            {
                if(k1==max(n,m))
                {
                    cout<<k1<<endl;
                    continue;
                }
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
        else
        {
            k-=((max(n,m)-1)+1);
            if(k<0)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define  MEM(a,x)   memset(a,x,sizeof(a))
#define  pi         acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  vpl    vector<pll>
#define  MX     100005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
int main()
{
   //freopen ("output.txt","w",stdout);
   //freopen ("input.txt","r",stdin);
    FIO;
    ll q;
    cin>>q;
    while(q--)
    {
        ll n,m,k1,k;
        cin>>n>>m>>k1;
        k=k1;
        if((n+m)%2==0)
        {
            if(k1%2==0)
            {
                cout<<k1<<endl;
                continue;
            }
            k-=((max(n,m)-1)+2);
            if(k<0)
            {
                if(k1==max(n,m))
                {
                    cout<<k1<<endl;
                    continue;
                }
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
        else
        {
            k-=((max(n,m)-1)+1);
            if(k<0)
            {
                cout<<-1<<endl;
                continue;
            }
            cout<<(max(n,m)-1)+k<<endl;
        }
    }
}


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

typedef long long ll;

int main() {
	ll q;cin>>q;
	ll x,y,k,steps;
	ll a[q],b[q],c[q];
	for(int i=0;i<q;i++)
	{
		cin>>a[i]>>b[i]>>c[i];
	}
	for(int i=0;i<q;i++)
	{
		steps = 0;
		x = a[i],y = b[i], k = c[i];
		ll temp = max(x,y);
		y = min(x,y);
		x = temp;
		if(k < x)
		{
			cout<<-1<<endl;
		}
		else if(x==k)
		{
			steps = x;
			if((x-y)%2)
			{
				steps--;
			}
			cout<<steps<<endl;
		}
		else
		{
			if(x == y)
			{
				if((k-x)%2)
				{
					k-=2;
				}
				cout<<k<<endl;
			}
			else
			{
				steps = (x);
				if((k-x)%2)
				{
					k--;
				}
				steps += (k-x);
				cout<<steps<<endl;
			}
		}

	}
}


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

typedef long long ll;

int main() {
	ll q;cin>>q;
	ll x,y,k,steps;
	vector<ll> a,b,c;
	for(int i=0;i<q;i++)
	{
		cin>>x>>y>>k;
		a.push_back(x);
		b.push_back(y);
		c.push_back(k);
	}
	for(int i=0;i<q;i++)
	{
		steps = 0;
		x = a[i],y = b[i], k = c[i];
		ll temp = max(x,y);
		y = min(x,y);
		x = temp;
		if(k < x)
		{
			cout<<-1<<endl;
		}
		else if(x==k)
		{
			steps = x;
			if((x-y)%2)
			{
				steps--;
			}
			cout<<steps<<endl;
		}
		else
		{
			if(x == y)
			{
				if((k-x)%2)
				{
					k-=2;
				}
				cout<<k<<endl;
			}
			else
			{
				if((x-y)%2 == 0)
				{
					//steps = k-2;
					steps += x;
					if((k-x) == 1)
					{
						steps--;
					}
					else
					{
						steps += (k-x);
						if((k-x)%2)
						{
							steps-=2;
						}
					}
				}
				else
				{
					steps = k-1;
					// if((k-x)%2 == 0)
					// {
					// 	steps = x-1;
					// 	steps += (k-x);
					// }
					// else
					// {
					// 	steps = x;
					// 	steps += k-(x+1);
					// }
					// steps += x;
					// if((k-(x+1)) == 1)
					// {
					// 	steps++;
					// }
					// else
					// {
					// 	steps += (k-(x+1));
					// 	if((k-(x+1))%2)
					// 	{
					// 		steps -= 2;
					// 	}
					// }
				}
				cout<<steps<<endl;
			}
		}

	}
}


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

typedef long long ll;

int main() {
	ll q;cin>>q;
	ll x,y,k,steps;
	vector<ll> a,b,c;
	for(int i=0;i<q;i++)
	{
		cin>>x>>y>>z;
		a.push_back(x);
		b.push_back(y);
		c.push_back(z);
	}
	for(int i=0;i<q;i++)
	{
		steps = 0;
		x = a[i],y = b[i], k = c[i];
		ll temp = max(x,y);
		y = min(x,y);
		x = temp;
		if(k < x)
		{
			cout<<-1<<endl;
		}
		else
		{
			if(x == y)
			{
				if((k-x)%2)
				{
					k-=2;
				}
				cout<<k<<endl;
			}
			else if(x==k)
			{
				steps = x;
				if((x-y)%2)
				{
					steps--;
				}
				cout<<steps<<endl;
			}
			else
			{
				steps = (x);
				if((k-x)%2)
				{
					k--;
				}
				steps += (k-x);
				cout<<steps<<endl;
			}
		}

	}
}


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

typedef long long ll;

int main() {
	ll q;cin>>q;
	ll x,y,k,steps;
	ll a[q],b[q],c[q];
	for(int i=0;i<q;i++)
	{
		cin>>a[i]>>b[i]>>c[i];
	}
	for(int i=0;i<q;i++)
	{
		steps = 0;
		x = a[i],y = b[i], k = c[i];
		ll temp = max(x,y);
		y = min(x,y);
		x = temp;
		if(k < x)
		{
			cout<<-1<<endl;
			//break;
		}
		else if(x==k)
		{
			steps = x;
			if((x-y)%2)
			{
				steps--;
			}
			cout<<steps<<endl;
		}
		else
		{
			if(x == y)
			{
				cout<<k-2<<endl;
			}
			else
			{
				steps = (x);
				// if((x-y)%2)
				// {
				// 	steps--;
				// }
				if((k-x)%2)
				{
					k--;
				}
				steps += (k-x);
				cout<<steps<<endl;
			}
		}

	}
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
//#define MAXN 500010
const int MAXN=200005;
struct node
{
	int x;
	int y;
}a[MAXN];
ll b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int t , n;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		b[0] = 0;
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 1 ; j <= n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a+1,a+n+1,cmp);
		sort(k+1,k+1+n);
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = b[i-1] + 1LL*a[i].y;
		}
		long long int r = 1e9;
		long long int l = 1;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k+1,k+n+1,mid)-k;
		    if(b[n]-b[loc-1]<=mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 500010
const int N=200100;
struct node
{
	int x;
	int y;
}a[MAXN];
int b[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int main()
{
	int t , n;
	cin >> t;
	while(t--)
	{
		scanf("%d",&n);
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d",&a[i].x);
		}
		for(int j = 0 ; j < n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a,a+n,cmp);
		b[0] = a[0].y;
		for(int i = 1 ; i < n ; i++)
		{
			b[i] = b[i-1] + a[i].y;
		}
		long long int r = 1000000000;
		long long int l = 0;
		while(l < r)
		{
			ll mid = (l+r)/2;
			ll num = 0;
			for(int i = 0 ; i < n ; i++)
			{
				if(a[i].x < mid)
				{
					continue;
				}
				else
				{
					if(i == 0)
					{
						num = num + b[n-1];
						break;
					}
					else
					{
						num = num + b[n-1] - b[i-1];
						break;
					}
				}
			}
			if(num >= mid)
			{
				l = mid+1;
				continue;
			}
			else
			{
				r = mid;
				continue;
			}
		}
		cout << l-1 << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 500010
const int N=200050;
struct node
{
	int x;
	int y;
}a[MAXN];
ll b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int t , n;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		b[0] = 0;
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 1 ; j <= n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a+1,a+n+1,cmp);
		sort(k+1,k+1+n);
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = b[i-1] + 1LL*a[i].y;
		}
		long long int r = 1e9;
		long long int l = 1;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k+1,k+n+1,mid)-k;
		    if(b[n]-b[loc-1]<=mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 500010
const int N=200100;
struct node
{
	int x;
	int y;
}a[MAXN];
int b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int main()
{
	int t , n;
	cin >> t;
	while(t--)
	{
		scanf("%d",&n);
		CL(k,0);
		CL(b,0);
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 0 ; j < n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a,a+n,cmp);
		sort(k,k+n);
		b[0] = a[0].y;
		for(int i = 1 ; i < n ; i++)
		{
			b[i] = b[i-1] + a[i].y;
		}
		long long int r = 1000000000;
		long long int l = 0;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k,k+n,mid)-k;
		    if(b[n-1]-b[loc-1]<=1LL*mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 500010
const int N=200100;
struct node
{
	int x;
	int y;
}a[MAXN];
ll b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int main()
{
	int t , n;
	cin >> t;
	while(t--)
	{
		scanf("%d",&n);
		b[0] = 0;
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 1 ; j <= n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a+1,a+n+1,cmp);
		sort(k+1,k+1+n);
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = b[i-1] + 1LL*a[i].y;
		}
		long long int r = 1000000000;
		long long int l = 1;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k+1,k+n+1,mid)-k;
		    if(b[n]-b[loc-1]<=1LL*mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
//#define MAXN 500010
const int MAXN=200100;
struct node
{
	int x;
	int y;
}a[MAXN];
int b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x < x2.x)
		return true;
	else
		return false;
}
int main()
{
	int t , n;
	cin >> t;
	while(t--)
	{
		scanf("%d",&n);
		CL(k,0);
		CL(b,0);
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 1 ; j <= n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a+1,a+n+1,cmp);
		sort(k+1,k+1+n);
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = b[i-1] + 1LL*a[i].y;
		}
		long long int r = 1000000000;
		long long int l = 1;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k+1,k+n+1,mid)-k;
		    if(b[n]-b[loc-1]<=1LL*mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
//#define MAXN 500010
const int MAXN=200005;
struct node
{
	int x;
	int y;
}a[MAXN];
ll b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x < x2.x)
		return true;
	else
		return false;
}
int t , n;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		b[0] = 0;
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 1 ; j <= n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a+1,a+n+1,cmp);
		sort(k+1,k+1+n);
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = b[i-1] + 1LL*a[i].y;
		}
		long long int r = 1e9;
		long long int l = 1;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k+1,k+n+1,mid)-k;
		    if(b[n]-b[loc-1]<=mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
//#define MAXN 500010
const int MAXN=200050;
struct node
{
	int x;
	int y;
}a[MAXN];
ll b[MAXN];
int k[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int t , n;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		b[0] = 0;
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&a[i].x);
			k[i]=a[i].x;
		}
		for(int j = 1 ; j <= n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a+1,a+n+1,cmp);
		sort(k+1,k+1+n);
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = b[i-1] + 1LL*a[i].y;
		}
		long long int r = 1e9;
		long long int l = 1;
		while(l < r)
		{
			ll mid = (l+r)>>1;
			ll num = 0;
			int loc = upper_bound(k+1,k+n+1,mid)-k;
		    if(b[n]-b[loc-1]<=mid)
			{
		        r = mid;
		    }
			else
			{
		        l = mid+1;
		    }
		}
		cout << l << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 500010
const int N=200100;
struct node
{
	int x;
	int y;
}a[MAXN];
bool cmp(node x1 , node x2)
{
	if(x1.x <= x2.x)
		return true;
	else
		return false;
}
int main()
{
	int t , n;
	cin >> t;
	while(t--)
	{
		scanf("%d",&n);
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d",&a[i].x);
		}
		for(int j = 0 ; j < n ; j++)
		{
			scanf("%d",&a[j].y);
		}
		sort(a,a+n,cmp);
		long long int r = 1000000000;
		long long int l = 0;
		while(l < r)
		{
			ll mid = (l+r)/2;
			ll num = 0;
			for(int i = 0 ; i < n ; i++)
			{
				if(a[i].x < mid)
				{
					continue;
				}
				else
				{
					num = num + a[i].y;
				}
			}
			if(num >= mid)
			{
				l = mid+1;
				continue;
			}
			else
			{
				r = mid;
				continue;
			}
		}
		cout << l-1 << endl;
	}
	return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

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

bool check(ll x){
  ll sum = 0;
  for(int i = 1; i <= n; i ++){
    if(a[i] > x)
      sum += b[i];
  }
  return sum <= x;
}

void solve()
{
  ll l = 0, r = 0;
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &a[i]);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &b[i]), r += b[i];

  while(l < r){
    int mid = l + r >> 1;
    if(check(mid))  r = mid;
    else  l = mid + 1;
  }
  printf("%lld\n", r);
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];
ll mx;

ll suf[maxn];

void init()
{
  for(int i = 0; i < maxn; i ++)  suf[i] = 0, x[i].a = 0, x[i].b = 0;
}

bool cmp(node x, node y){
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0;
  init();
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
    }
    else{
      l = mid + 1;
    }
  }
  //cout << "---" << r << endl;
  printf("%lld\n", min(mx, x[r].a));
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


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

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];
ll mx;

ll suf[maxn];

bool cmp(node x, node y){
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0;
  memset(suf, 0, sizeof(suf));
  memset(x, 0, sizeof(x));
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
    }
    else{
      l = mid + 1;
    }
  }
  //cout << "---" << r << endl;
  printf("%lld\n", min(mx, x[r].a));
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];
ll mx;

ll suf[maxn];

bool cmp(node x, node y){
  if(x.a == y.a){
    return x.b > y.b;
  }
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0;
  memset(suf, 0, sizeof(suf));
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
      mx = min(mx, max(x[r].a, suf[r + 1]));
    }
    else{
      l = mid + 1;
      mx = min(mx, max(x[l].a, suf[l + 1]));
    }
  }
  //cout << "---" << r << endl;
  printf("%lld\n", mx);
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];
ll mx;

ll suf[maxn];

void init(int n)
{
  for(int i = 0; i <= n; i ++)  suf[i] = 0, x[i].a = 0, x[i].b = 0;
}

bool cmp(node x, node y){
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0;
  init(n);
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
    }
    else{
      l = mid + 1;
    }
  }
  //cout << "---" << r << endl;
  printf("%lld\n", min(mx, x[r].a));
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];

ll suf[maxn];

bool cmp(node x, node y){
  if(x.a == y.a){
    return x.b > y.b;
  }
  return x.a < y.a;
}

void solve()
{
  ll sum = 0, mx = 0;
  for(int i = 0; i <= n + 1; i ++)  suf[i] = 0;
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  for(int i = 1; i <= n; i ++){
    mx = min(mx, max(x[i].a, suf[i + 1]));
  }

  printf("%lld\n", mx);
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];

ll suf[maxn];

bool cmp(node x, node y){
  if(x.a == y.a){
    return x.b > y.b;
  }
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0, mx = 0;
  memset(suf, 0, sizeof(suf));
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
      mx = min(mx, max(x[r].a, suf[r + 1]));
    }
    else{
      l = mid + 1;
      mx = min(mx, max(x[l].a, suf[l + 1]));
    }
  }
  //cout << "---" << r << endl;
  printf("%lld\n", mx);
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


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

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];
ll mx;

ll suf[maxn];

bool cmp(node x, node y){
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0;
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
    }
    else{
      l = mid + 1;
    }
  }
  printf("%lld\n", min(mx, x[r].a));
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];
ll mx;

ll suf[maxn];

bool cmp(node x, node y){
  if(x.a == y.a){
    return x.b > y.b;
  }
  return x.a < y.a;
}

bool check(int p){
  return x[p].a >= suf[p + 1];
}

void solve()
{
  ll sum = 0;
  memset(suf, 0, sizeof(suf));
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  int l = 1, r = n;
  while(l < r){
    int mid = l + r >> 1;
    if(check(mid)){
      r = mid;
    }
    else{
      l = mid + 1;
    }
  }
  //cout << "---" << r << endl;
  printf("%lld\n", min(mx, x[r].a));
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

int t, n;
struct node{
  ll a, b;
}x[maxn];

ll suf[maxn];

bool cmp(node x, node y){
  if(x.a == y.a){
    return x.b > y.b;
  }
  return x.a < y.a;
}

void solve()
{
  ll sum = 0, mx = 0;
  memset(suf, 0, sizeof(suf));
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].a), mx = max(mx, x[i].a);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &x[i].b), sum += x[i].b;
  mx = min(mx, sum);
  sort(x + 1, x + n + 1, cmp);
  for(int i = n; i >= 1; i --)  suf[i] = suf[i + 1] + x[i].b;

  for(int i = 1; i <= n; i ++){
    mx = min(mx, max(x[i].a, suf[i + 1]));
  }

  printf("%lld\n", mx);
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;

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

bool check(ll x){
  ll sum = 0;
  for(int i = 1; i <= n; i ++){
    if(a[i] > x)
      sum += b[i];
  }
  return sum <= x;
}

void solve()
{
  ll l = 0, r = 0;
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &a[i]);
  for(int i = 1; i <= n; i ++)  scanf("%lld", &b[i]), r += b[i];

  while(l < r){
    ll mid = l + r >> 1;
    if(check(mid))  r = mid;
    else  l = mid + 1;
  }
  printf("%lld\n", r);
}

int main()
{
  scanf("%d", &t);
  while(t --){
    solve();
  }
  return 0;
}


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

bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int tt;
	cin >> tt;
	while(tt--){
		int n;
		cin >> n;
		vector<lli> a(n), b(n);
		for(int i=0;i<n; i++) cin >> a[i];
		for(int i=0; i<n; i++) cin >> b[i];
		vector<pair<lli,lli>> vec(n);
		for(int i=0;i<n; i++){
			vec[i] = make_pair(a[i], b[i]);
		}
		sort(vec.begin(), vec.end(), compare);
		lli ans = vec[0].first;
		lli sum = 0;
		int i=0;
		for(i=1; i<vec.size(); i++){
			sum += vec[i-1].second;
			if(max(sum, vec[i].first) <= ans){
				ans = max(sum, vec[i].first);
			}
			else break;
		}
		if(i==vec.size()){
			sum += vec.back().second;
			ans = min(ans, sum);
		}
		cout << ans << "\n";
	}
	return 0;
}


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

bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int tt;
	cin >> tt;
	while(tt--){
		int n;
		cin >> n;
		vector<lli> a(n), b(n);
		for(int i=0;i<n; i++) cin >> a[i];
		for(int i=0; i<n; i++) cin >> b[i];
		vector<pair<lli,lli>> vec(n);
		for(int i=0;i<n; i++){
			vec[i] = make_pair(a[i], b[i]);
		}
		sort(vec.begin(), vec.end(), compare);
		lli ans = vec[0].first;
		lli sum = 0;
		int i=0;
		for(i=1; i<vec.size(); i++){
			sum += vec[i-1].second;
			if(max(sum, vec[i].first) <= ans){
				ans = max(sum, vec[i].first);
			}
			else break;
		}
		if(i==vec.size()){
			sum += vec.back().second;
			ans = min(ans, sum);
		}
		cout << ans << "\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define ull unsigned long long int
#define ld long double
const int mod = 1e9+7;
const int maxN = 2*1e6+5;

bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int tt;
	cin >> tt;
	while(tt--){
		int n;
		cin >> n;
		vector<lli> a(n), b(n);
		for(int i=0;i<n; i++) cin >> a[i];
		for(int i=0; i<n; i++) cin >> b[i];
		vector<pair<lli,lli>> vec(n);
		for(int i=0;i<n; i++){
			vec[i] = make_pair(a[i], b[i]);
		}
		sort(vec.begin(), vec.end(), compare);
		lli ans = vec[0].first;
		lli sum = 0;
		int i;
		for(i=1; i<vec.size(); i++){
			sum += vec[i-1].second;
			if(max(sum, vec[i].first) <= ans){
				ans = max(sum, vec[i].first);
			}
			else break;
		}
		if(i==vec.size()){
			sum += vec.back().second;
			ans = min(ans, sum);
		}
		cout << ans << "\n";
	}
	return 0;
}


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

bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int tt;
	cin >> tt;
	while(tt--){
		int n;
		cin >> n;
		vector<lli> a(n), b(n);
		for(int i=0;i<n; i++) cin >> a[i];
		for(int i=0; i<n; i++) cin >> b[i];
		vector<pair<lli,lli>> vec(n);
		for(int i=0;i<n; i++){
			vec[i] = make_pair(a[i], b[i]);
		}
		sort(vec.begin(), vec.end(), compare);
		lli ans = vec[0].first;
		lli sum = 0;
		int i=0;
		for(i=1; i<vec.size(); i++){
			sum += vec[i-1].second;
			if(max(sum, vec[i].first) < ans){
				ans = max(sum, vec[i].first);
			}
			else break;
		}
		if(i==vec.size()){
			sum += vec.back().second;
			ans = min(ans, sum);
		}
		cout << ans << "\n";
	}
	return 0;
}


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

bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int tt;
	cin >> tt;
	while(tt--){
		int n;
		cin >> n;
		vector<lli> a(n), b(n);
		for(int i=0;i<n; i++) cin >> a[i];
		for(int i=0; i<n; i++) cin >> b[i];
		map<lli,lli, greater<lli>> mp;
		for(int i=0; i<n; i++){
			mp[a[i]] += b[i];
		}
		lli ans = 1e15;
		lli prev1 = 0;
		for(auto itr = mp.begin(); itr!=mp.end(); itr++){
			ans = min(ans, max(prev1, itr->first));
			prev1 += itr->second;
		}
		ans = min(ans, prev1);
		cout << ans <<"\n";
	}
	return 0;
}


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

bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

vector<pair<lli,lli>> vec(3*1e5+5);

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int tt;
	cin >> tt;
	while(tt--){
		int n;
		cin >> n;
		vector<lli> a(n), b(n);
		for(int i=0;i<n; i++) cin >> a[i];
		for(int i=0; i<n; i++) cin >> b[i];
		for(int i=0;i<n; i++){
			vec[i] = make_pair(a[i], b[i]);
		}
		sort(vec.begin(), vec.begin()+n, compare);
		lli ans = vec[0].first;
		lli sum = 0;
		int i;
		for(i=1; i<vec.size(); i++){
			sum += vec[i-1].second;
			if(max(sum, vec[i].first) <= ans){
				ans = max(sum, vec[i].first);
			}
			else break;
		}
		if(i==vec.size()){
			sum += vec.back().second;
			ans = min(ans, sum);
		}
		cout << ans << "\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define ull unsigned long long int
#define ld long double
const int mod = 1e9+7;
const int maxN = 2*1e6+5;

void solve();
bool compare(pair<lli,lli>& p1, pair<lli,lli>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int tt;
	cin >> tt;
	while(tt--) solve();
	return 0;
}

void solve(){
	int n;
	cin >> n;
	vector<lli> a(n), b(n);
	for(int i=0;i<n; i++) cin >> a[i];
	for(int i=0; i<n; i++) cin >> b[i];
	vector<pair<lli,lli>> vec;
	for(int i=0;i<n; i++){
		vec.push_back(make_pair(a[i], b[i]));
	}
	sort(vec.begin(), vec.end(), compare);
	lli ans = vec[0].first;
	lli sum = 0;
	for(int i=1; i<vec.size(); i++){
		sum += vec[i-1].second;
		ans = min(ans, max(sum, vec[i].first));
	}
	sum += vec.back().second;
	ans = min(ans, sum);
	cout << ans << "\n";
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define ull unsigned long long int
#define ld long double
const int mod = 1e9+7;
const int maxN = 2*1e6+5;

void solve();
bool compare(pair<int,int>& p1, pair<int,int>& p2){
	if(p1.first > p2.first) return true;
	else if(p1.first < p2.first) return false;
	else return p1.second <= p2.second;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int tt;
	cin >> tt;
	while(tt--) solve();
	return 0;
}

void solve(){
	int n;
	cin >> n;
	vector<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];
	vector<pair<int,int>> vec;
	for(int i=0;i<n; i++){
		vec.push_back(make_pair(a[i], b[i]));
	}
	sort(vec.begin(), vec.end(), compare);
	int ans = vec[0].first;
	int sum = 0;
	for(int i=1; i<vec.size(); i++){
		sum += vec[i-1].second;
		ans = min(ans, max(sum, vec[i].first));
	}
	sum += vec.back().second;
	ans = min(ans, sum);
	cout << ans << "\n";
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<ll> a(200000), b(200000);
	vector<pair<ll, ll>> v(200000);

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

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

	std::sort(v.begin(), v.begin() + n);

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = 0; i < n - 1; i++) ans = min(ans, max(v[i].first, v[i + 1].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<ll> a(n), b(n);
	vector<pair<ll, ll>> v;

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

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

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

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = 0; i < n - 1; i++) ans = min(ans, max(v[i].first, v[i + 1].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<ll> a(200000), b(200000);
	vector<pair<ll, ll>> v(200000);

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

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

	std::sort(v.begin(), v.begin() + n);

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = n - 1; i >= 0; i--) ans = min(ans, max(v[i].first, v[i].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t, a, b;
string s;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<ll> a(200000), b(200000);
	vector<pair<ll, ll>> v(200000);

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

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

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

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = n - 1; i >= 0; i--) ans = min(ans, max(v[i].first, v[i].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<ll> a(200000), b(200000);
	vector<pair<ll, ll>> v(200000);

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

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

	std::sort(v.begin(), v.begin() + n);

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = n - 1; i >= 0; i--) ans = min(ans, min(v[i].first, v[i].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t, a, b;
string s;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<pair<ll, ll>> v;

	for(int i = 0; i < n; i++)
	{
		cin >> v[i].first >> v[i].second;
	}

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

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = n - 1; i >= 0; i--) ans = min(ans, max(v[i].first, v[i].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t;

vector<ll> a(200000), b(200000);

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<pair<ll, ll>> v;

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

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

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

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = 0; i < n - 1; i++) ans = min(ans, max(v[i].first, v[i + 1].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    return 0;
}


#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int t, a, b;
string s;

inline void input()
{
	ios::sync_with_stdio(0);
	cin >> t;
}

inline void do_stuff()
{
	int n;
	cin >> n;

	vector<pair<ll, ll>> v(200000);

	for(int i = 0; i < n; i++)
	{
		cin >> v[i].first >> v[i].second;
	}

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

	ll tmp = 0;
	for (int i = 0; i < n; i++) tmp += v[i].second;

	ll ans = min(tmp, v[n - 1].first);

	for (int i = n - 2; i >= 0; i--) v[i].second += v[i + 1].second;

	for (int i = n - 1; i >= 0; i--) ans = min(ans, max(v[i].first, v[i].second));

	cout << ans << endl;
}

inline void output()
{

}

int main()
{
	input();

	for (int i = 0; i < t; i++)
	{
		do_stuff();
	}

	output();

    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;
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];
		}
	}
	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
			if(check(c[mid]))	r = mid;
			else l = mid+1;
		}
		cout<<ans<<endl;

	}

	return 0;
}