#include<iostream>
using namespace std;
int a[7][8], c[7][8], T, re, d[7][7];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
bool checkXY(int x, int y) {
if(x < 0 || x >= 7 || y < 0 || y >= 8)
return false;
return true;
}
void doing(int k) {
/*cout << "HK " << k << endl;*/
if(k == 28) {
/*cout << "HK" << endl;*/
re++;
return;
}
int x = -1, y = -1;
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 8; j++) {
if(c[i][j] == 0) {
x = i; y = j;
break;
}
}
if(x != -1)
break;
}
for(int i = 0; i < 4; i++) {
int newx = x + dx[i];
int newy = y + dy[i];
if(checkXY(newx, newy) && c[newx][newy] == 0) {
if(d[a[x][y]][a[newx][newy]] == 0) {
d[a[x][y]][a[newx][newy]] = 1;
d[a[newx][newy]][a[x][y]] = 1;
c[x][y] = 1;
c[newx][newy] = 1;
doing(k + 1);
d[a[x][y]][a[newx][newy]] = 0;
d[a[newx][newy]][a[x][y]] = 0;
c[x][y] = 0;
c[newx][newy] = 0;
}
}
}
}
int main() {
/*freopen("input.txt", "r", stdin);*/
cin >> T;
for(int t = 0; t < T; t++) {
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 8; j++) {
cin >> a[i][j];
}
}
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 7; j++) {
d[i][j] = 0;
}
}
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 8; j++) {
c[i][j] = 0;
}
}
re = 0;
doing(0);
cout << "Case #" << t + 1 << endl << re << endl;
}
return 0;
}