#include using namespace std; typedef long long ll; const int N = 1e5+10, mod = 1e9+7; int a[60][60]; bool st[60]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin>>t; while(t--) { memset(st, 0, sizeof st); int n; cin>>n; int lr = 0; for(int i = 1; i <= n; i++) { int x; cin>>x; if(x == 1) { lr = i; } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { cin>>a[i][j]; } } int cnt = n; int f = 1; int r = a[lr][1]; while(true) { // cout<