#include using ll = long long; template using vc = std::vector; template using vvc = std::vector>; #define for_(i, l, r, v...) for (ll i = (l), i##e = (r), ##v; i <= i##e; ++i) template Tp dec(Tp &i) { return --i; } template Tp inc(Tp &i) { return ++i; } using namespace std; void solve(int t_ = 0) { int n; cin >> n; vvc g(n), ng(n); vc deg(n); vc vs(n); for (auto &i : vs) cin >> i; for_(i, 0, n - 1) for_(j, 0, n - 1) { if (vs[i][j] & 1) { g[i].push_back(j); ng[j].push_back(i); ++deg[j]; } } vc> s(n); vc vis(n); for_(cnt, 1, n) { int j; for (j = 0; j < n; ++j) { if (vis[j]) continue; if (!deg[j]) break; } vis[j] = 1; for (auto &&i : g[j]) --deg[i]; s[j].insert(cnt); for (auto &&i : ng[j]) set_union(s[j].begin(), s[j].end(), s[i].begin(), s[i].end(), inserter(s[j], s[j].end())); } for (auto &&a : s) { cout << a.size(); for (auto &&i : a) cout << ' ' << i; cout << '\n'; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cerr << std::fixed << std::setprecision(6); int i_ = 0; int t_ = 0; std::cin >> t_; for (i_ = 0; i_ < t_; ++i_) solve(i_); return 0; }