#include <iostream>
#include <memory.h>
#define IN (1<<28)
using namespace std;
int kase,N;
int G[505][505];
int locost[505], visited[505];
int cost, ans;
void prime()
{
for( int i = 1; i <= N; i++ )
locost[i] = G[1][i];
locost[1] = 0;
visited[1] = 1;
int cnt = 0;
while( cnt != N - 1 )
{
int MinV = 0, Min = IN;
for( int i = 1; i <= N; i++ )
if( locost[i] && locost[i] < Min )
{
MinV = i;
Min = locost[i];
}
if( !MinV )
break;
cost += Min;
cnt++;
ans = max(ans, Min);
locost[MinV] = 0;
for( int i = 1; i <= N; i++ )
{
if( locost[i] && locost[i] > G[MinV][i] )
locost[i] = G[MinV][i];
}
}
}
int main()
{
cin >> kase;
while( kase-- )
{
cin >> N;
memset(G, 0, sizeof(G));
memset(locost, 0, sizeof(locost));
memset(visited, 0, sizeof(visited));
for( int i = 1; i <= N; i++ )
for( int j = 1; j <= N; j++ )
cin >> G[i][j];
cost = 0;
ans = 0;
prime();
cout << ans << endl;
}
return 0;
}