#include<iostream>
#include<queue>
using namespace std;
class map
{
int visit[20];
int juzhen[20][20];
int dianshu;
public:
void setjuzhen(int shu, int shuru[20][20])
{
int i, j;
dianshu = shu;
for (i = 0; i < 20; i++)
for (j = 0; j < 20; j++)
juzhen[i][j] = 0;
for (i = 0; i < dianshu; i++)
for (j = 0; j < dianshu; j++)
juzhen[i][j] = shuru[i][j];
}
void dfs(int v)
{
int i, k;
visit[v] = 1;
cout << v << " ";
int *jindian=new int[dianshu];
for (i = 0; i < dianshu; i++)
jindian[i] = -1;
k = 0;
for (i = 0; i < dianshu; i++)
{
if (juzhen[v][i] == 1)
jindian[k++] = i;
}
i = 0;
for (i = 0; i < k; i++)
if (visit[jindian[i]] == 0)
dfs(jindian[i]);
delete[]jindian;
}
void dfsru()
{
int v;
for (v = 0; v < dianshu; v++)
visit[v] = 0;
for (v = 0; v < dianshu; v++)
if (visit[v] == 0)
dfs(v);
cout << endl;
}
void bfs(int v)
{
int u;
int i, k;
int *jindian = new int[dianshu];
queue<int>q;
for (i = 0; i < dianshu; i++)
visit[i] = 0;
for (v = 0; v < dianshu; v++)
{
if (!visit[v])
{
visit[v] = 1;
q.push(v);
while (!q.empty())
{
u = q.front();
q.pop();
cout << u << " ";
k = 0;
for (i = 0; i < dianshu; i++)
{
if (juzhen[u][i] == 1)
jindian[k++] = i;
}
i = 0;
for (i = 0; i < k; i++)
if (visit[jindian[i]] == 0)
{
q.push(jindian[i]);
visit[jindian[i]] = 1;
}
}
}
}
cout << endl;
delete[]jindian;
}
void bfsru()
{
bfs(0);
}
};
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int i, j;
int shuru[20][20];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin>>shuru[i][j];
map tu;
tu.setjuzhen(n, shuru);
tu.bfsru();
}
}
(by 归忆)