#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
 
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
 
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      fill(a)           memset(a, 0, sizeof (a))

#define      rep(i, n)         for(ll i = 0; i < (n); ++i)
#define      repA(i, a, n)     for(ll i = a; i <= (n); ++i)
#define      repD(i, a, n)     for(ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) <<endl
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
#define fbo find_by_order
#define ook order_of_key
 
ll power(ll x,ll y){ll res=1ll; while(y>0){if(y&(1ll)) res=res*x;y=y>>(1ll);x=x*x;}return res;} // modular exponent
 
int main()
{
    ios_base::sync_with_stdio(false); // don't use printf and scanf
    cin.tie(NULL); // cout<<fixed<<setprecision
	int n;cin>>n;
	vector<vector<int>> v(n,vector<int>(5,0));
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<5;j++) cin>>v[i][j];
	}
	// now we will do binary search by answer
	int hi=1e9;int lo=1;
	while(hi>lo)
	{
		int mid=(lo+hi+1)/2;
		// we need the biggest possible
		vector<int> b(n,0);
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<5;j++)
			{
				if(v[i][j]>=mid) b[i]|=(1<<j);
			}
		}
		vector<vector<int>> dp(4,vector<int>((1<<5),0));
		dp[0][0]=1;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<=2;j++)
			{
				for(int k=0;k<(1<<5);k++)
				{
					if(dp[j][k])
					{
						dp[j+1][k|b[i]]=1;
					}
				}
			}
		}
		if(dp[3][(1<<5)-1]) lo=mid;
		else hi=mid-1;
	}
    cout<<lo<<'\n';
	return 0;
}
// JUST ASK YOURSELF DID YOU GIVE YOUR BEST ? ISSE ZYADA
// KUCH KAR BHI NAHI SAKTE !! ENJOY AND GIVE YOUR BEST!!



/*
	* 	Created By:    'Present_Sir'
	* 	Created On:    02.05.2021 10:06:31
*/

#include<bits/stdc++.h>
using namespace std;
 
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL)
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sz(v) ((int)v.size())
#define int long long
//#define i_n_p_u_t(t) cin >> t
#define i_n_p_u_t(t) t = 1
 
void solve(){                            
	int n;
	cin >> n;
	
	vector < vector < int > >  v(n);
	for(int i=0; i<n; ++i){
		int a, b, c, d, e;
		cin >> a >> b >> c >> d >> e;
		v[i] = {a, b, c, d, e};
	}
    
    auto check = [&](int mid){
        vector < int > t;
        map < int, int > mp;
        for(int i=0; i<n; ++i){
            int  _s=0;
            for(int j=0; j<5; ++j){
                if(v[i][j] >= mid){
                    _s = ( (_s << 1) | 1 );
                }else{
                    _s = _s << 1;
                }
            }
            if(mp.count(_s) and mp[_s]==3)continue;
            t.push_back(_s);
            mp[_s] += 1;
        }
        
        for(int i=0; i<sz(t); ++i){
            for(int j=i+1; j<sz(t); ++j){
                for(int k=j+1; k<sz(t); ++k){
                    bool ok = true;
                    for(int p=0; p<5; ++p){
                        if( ( t[i]&(1ll<<p) ) == 0 and ( t[j]&(1ll<<p) ) == 0 and ( t[k]&(1ll<<p) ) == 0)ok=false;
                    }
                    if(ok) return true;
                }
            }
        }
        return false;
    };

    int l = 1, r = 1e9+5;
    while(l<=r){
        int mid = (l+r) >> 1;
        if(check(mid)){
            l = mid+1;
        }else{
            r = mid-1;
        }
    }

    int ans = l-1;
    cout << ans << "\n";
	
}
 
int32_t main(){
    IOS;
 
    int t;
    i_n_p_u_t(t);
    
    for(int i=1; i<=t; ++i){
        // cout << "Case #" << i << ": ";
		solve();
    }
    return 0;
}
