#include <iostream>
#include <cstring>

using namespace std;

int orig[7][6];
int curr[7][6];
int mmm[7];
bool seen[7];
int e1[7] = {0, 3, 4, 5, 0, 1, 2}; // to center
int e2[7] = {0, 2, 3, 4, 5, 0, 1}; // to cw neighbor
int e3[7] = {0, 4, 5, 0, 1, 2, 3}; // to ccw neighbor
bool dfs(int n) {
    if (n == 7) {
        for (int i = 0; i < 7; i++) {
            cout << " " << mmm[i];
        }
        cout << endl;
        /*
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 6; j++) {
                cerr << curr[i][j] << " ";
            }
            cerr << endl;
        }
        */
        return true;
    }
    for (int i = 0; i < 7; i++) {
        if (seen[i]) continue;
        mmm[n] = i;
        seen[i] = true;
        if (n == 0) {
            int j = 0;
            while (orig[i][j] != 1) j++;
            for (int k = 0; k < 6; k++) {
                curr[n][k] = orig[i][(j+k)%6];
            }
            if (dfs(n+1)) return true;
        } else {
            for (int j = 0; j < 6; j++) {
                for (int k = 0; k < 6; k++) {
                    curr[n][k] = orig[i][(j+k)%6];
                }
                // check center
                if (curr[n][e1[n]] != curr[0][n-1]) continue;
                // check prev
                if (n > 1 && curr[n][e3[n]] != curr[n-1][e2[n-1]]) continue;
                if (n == 6 && curr[n][e2[n]] != curr[1][e3[1]]) continue;
                if (dfs(n+1)) return true;
            }
        }
        seen[i] = false;
    }
    return false;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int ncase; cin >> ncase;
    for (int csnum = 1; csnum <= ncase; csnum++) {
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 6; j++) {
                cin >> orig[i][j];
            }
        }
        cout << "Case " << csnum << ":";
        memset(seen, 0, sizeof seen);
        if (!dfs(0)) {
            cout << " No solution" << endl;
        }
    }
}