#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <map>using namespace std;int n,q[1000],inq[1000],con[1000][9],move[1000][9],sta[9];map<int,int> pos; bool solve(){ int head = 0,tail = 0; inq[q[++tail] = 1] = 1; while (head < tail){ int now = q[++head]; for (int i = 1;i <= 8;i ++) {int next = pos[con[now][i]]; if (con[now][i] && pos[con[next][((i + 1) ^ 1) - 1]] != now) return false; if (con[now][i] && !inq[pos[con[now][i]]]){ inq[q[++tail] = next] = 1; for (int j = 1;j <= 8;j ++) move[next][j] = move[now][j]; move[next][i] = move[now][i] + 1;} } } for (int i = 1;i <= n;i ++) if (!inq[i]) return false; return true;}int main(){ int T; cin>>T; while (T--){ cin>>n; pos.clear(); pos[0] = 0; for (int i = 1;i <= n;i ++){int o;scanf("%d",&o);pos[o] = i; for (int j = 1;j <= 8;j ++){ int t; scanf("%d",&t); con[i][j] = t;} } memset(inq,0,sizeof inq); memset(move,0,sizeof move); if (!solve()) {puts("Inconsistent");continue;} memset(sta,0,sizeof sta); for (int i = 1;i <= n;i ++){for (int j = 1;j <= 8;j ++){ if (move[i][j] > sta[j]) sta[j] = move[i][j]; } } cout<<(sta[1] + sta[2] + 1) * (sta[3] + sta[4] + 1) * (sta[5] + sta[6] + 1) * (sta[7] + sta[8] + 1)<<endl; } return 0;}