#include <iostream>
using namespace std;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int st[2000];
int top;
void push(int x){
top++;
st[top] = x;
}
int pop(){
int x = st[top];
top--;
return x;
}
int main(){
freopen("input.txt","r",stdin);
int T,x;
cin>>T;
for (x = 0; x < T; x++){
int N;
cin>>N;
int A[100][200],i,j;
int B[100][100] = {0};
for (i = 0; i < N - 1; i++){
for (j = 0; j < N *2; j++){
cin>> A[i][j];
}
}
for (i = 0; i < N -1; i++){
for (j = 0; j < N*2; j+=2){
B[A[i][j]-1][A[i][j+1]-1] = i+1;
}
}
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if (B[i][j] == 0)
B[i][j] = N;
}
}
int check = 1;
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if (B[i][j] != 0){
int so = B[i][j];
int d = 0;
top = -1;
push(i);
push(j);
A[i][j] = 0;
d++;
while (top>=1){
int col = pop();
int row = pop();
for (int k = 0; k <4; k++){
int x1 = row + dx[k];
int y1 = col + dy[k];
if (x1>=0 && x1 < N && y1 >= 0 && y1 < N && B[x1][y1] == so){
push(x1);
push(y1);
B[x1][y1] = 0;
d++;
}
}
}
if (d < N){
check = 0;
break;
}
}
if (check == 0) break;
}
}
cout<<"Case #"<<x+1<<endl;
if (check ==1) cout<<"good"<<endl;
else cout<<"wrong"<<endl;
}
return 0;
}
/*
12
2
1 2 2 1
5
1 1 1 2 1 3 3 2 2 2
2 1 4 2 4 1 5 1 3 1
4 5 5 2 5 3 5 5 5 4
2 5 3 4 3 5 4 3 4 4
5
1 1 1 2 1 3 3 2 2 2
2 1 3 1 4 1 5 1 4 2
4 5 5 2 5 3 5 5 5 4
2 4 1 4 3 5 4 3 4 4
2
1 2 2 1
5
1 1 1 2 1 3 3 2 2 2
2 1 4 2 4 1 5 1 3 1
4 5 5 2 5 3 5 5 5 4
2 5 3 4 3 5 4 3 4 4
5
1 1 1 2 1 3 3 2 2 2
2 1 3 1 4 1 5 1 4 2
4 5 5 2 5 3 5 5 5 4
2 4 1 4 3 5 4 3 4 4
2
1 2 2 1
5
1 1 1 2 1 3 3 2 2 2
2 1 4 2 4 1 5 1 3 1
4 5 5 2 5 3 5 5 5 4
2 5 3 4 3 5 4 3 4 4
5
1 1 1 2 1 3 3 2 2 2
2 1 3 1 4 1 5 1 4 2
4 5 5 2 5 3 5 5 5 4
2 4 1 4 3 5 4 3 4 4
2
1 2 2 1
5
1 1 1 2 1 3 3 2 2 2
2 1 4 2 4 1 5 1 3 1
4 5 5 2 5 3 5 5 5 4
2 5 3 4 3 5 4 3 4 4
5
1 1 1 2 1 3 3 2 2 2
2 1 3 1 4 1 5 1 4 2
4 5 5 2 5 3 5 5 5 4
2 4 1 4 3 5 4 3 4 4
*/