import sys
FILE = False
if FILE:
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def get_int():
return int(sys.stdin.readline())
def get_string():
return sys.stdin.readline().strip()
test_cases= get_int()
for test in range(test_cases):
n = get_int()
matrix = [""] * (n + 1)
sets = [set() for i in range(n+1)]
for i in range(1,n + 1):
input = get_string()
matrix[i] = " " + input
for j in range(1,n+1):
if matrix[i][j] == "0" and i!= j:
continue
sets[j].add(i)
for i in range(1,n+1):
print(len(sets[i]), *sets[i])
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
#define ll long long
#define FIO ios::sync_with_stdio(false),cout.tie(0),cin.tie(0);
#define lp(i,n) for(ll i=0;i<n;i++)
#define lp1(i,n) for(ll i=1;i<=n;i++)
#define sz(x) sizeof(x)/sizeof(x[0])
#define szvi(vec) vec.size()
#define vi vector<int>
#define vvc vector<vector<char>>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define pb(x) push_back(x)
queue<ll>perm;
const int mod = 32768;
const int N=10e2+7;
vector<int>adj[N];
vector<int>vis(N);
vi parent(N);
int n;
set<int>subset[N];
#define f(i,s,e) for(int i=s;i<e;++i)
#define feq(i,s,e) for(int i=s;i<=e;++i)
ll ans=0;
int cnt;
void dfs(int v){
vis[v]=1;
for(auto a:adj[v]){
if(!vis[a]){
dfs(a);
}
for (auto j = subset[a].begin(); j != subset[a].end(); j++)
subset[v].insert((*j));
}
subset[v].insert(cnt++);
}
void solve(){
cnt=1;
cin>>n;
char ch;
parent=vi(n+1,0);
vis=vi(n+1,0);
f(i,1,n+1){
adj[i].clear();
subset[i].clear();
}
f(i,1,n+1){
f(j,1,n+1){
cin>>ch;
if(ch=='1'){
adj[j].pb(i);
parent[i]++;
}
}
}
feq(i,1,n)
if(!parent[i])
dfs(i);
feq(i,1,n){
cout<<subset[i].size()<<" ";
for (auto j = subset[i].begin(); j != subset[i].end(); j++)
cout << (*j) << " ";
cout<<endl;
}
return;
}
int main()
{
FIO
ll tc=1;
cin>>tc;
while(tc--){
solve();
}
}
1334B - Middle Class | 260C - Balls and Boxes |
1554A - Cherry | 11B - Jumping Jack |
716A - Crazy Computer | 644A - Parliament of Berland |
1657C - Bracket Sequence Deletion | 1657B - XY Sequence |
1009A - Game Shopping | 1657A - Integer Moves |
230B - T-primes | 630A - Again Twenty Five |
1234D - Distinct Characters Queries | 1183A - Nearest Interesting Number |
1009E - Intercity Travelling | 1637B - MEX and Array |
224A - Parallelepiped | 964A - Splits |
1615A - Closing The Gap | 4C - Registration System |
1321A - Contest for Robots | 1451A - Subtract or Divide |
1B - Spreadsheet | 1177A - Digits Sequence (Easy Edition) |
1579A - Casimir's String Solitaire | 287B - Pipeline |
510A - Fox And Snake | 1520B - Ordinary Numbers |
1624A - Plus One on the Subset | 350A - TL |