#include <iostream>
#include <queue>
using namespace std;

int N;
int G[102][102];
int new_G[102][102];

int check_ft[102];	//Fortress_i;
void khoitao ()
{
	for (int i=1; i<=N; i++)
	{
		check_ft[i]=0;
	}
}

void BFS_queue (int u)
{
	queue <int> Q;
	Q.push(u);
	check_ft[u]=1;
	while (!Q.empty())
	{
		int value = Q.front();
		Q.pop();
		for (int i=1; i<=N; i++)
		{
			if (new_G[value][i]==1 && check_ft[i]==0)
			{
				check_ft[i]=1;
				Q.push(i);
			}
		}
	}
}

int main ()
{
	//IN;
	int t;
	cin>>t;
	for (int k=1; k<=t; k++)
	{
		cin>>N;
		for (int i=1; i<=N; i++)
		{
			for (int j=1; j<=N; j++)
			{
				cin>>G[i][j];
				new_G[i][j]=G[i][j];
			}
		}
		//OUT;
		//Xac dinh slt ban dau;
		int sltMax=0;
		khoitao ();
		for (int i=1; i<=N; i++)
		{
			if (check_ft[i]==0)
			{
				sltMax++;
				BFS_queue (i);
			}
		}
		//Slt moi
		int ft=0;
		int sltMax_New=0;
		int isDestroy=0;
		for (int i=1; i<=N; i++)
		{
			int xoa=i;
			for (int r=1; r<=N; r++)
			{
				for (int c=1; c<=N; c++)
				{
					if (r==xoa || c==xoa) new_G[r][c]=0;
					else new_G[r][c]=G[c][r];
				}
			}
			khoitao ();
			int slt=0;
			for (int j=1; j<=N; j++)
			{
				if (check_ft[j]==0 && j!=xoa)
				{
					slt++;
					BFS_queue (j);
				}
			}
			if (slt!=sltMax && slt>sltMax_New) 
			{
				isDestroy=1;
				sltMax_New=slt;
				ft=xoa;
			}
		}
		if (isDestroy==1) cout<<ft<<endl;
		else cout<<"0"<<endl;
	}
	return 0;
}