// Arup Guha
// 7/22/2013
// 2012 East Central Regional Problem C: Hexagon Perplexagon

import java.util.*;

public class c {

	final public static int NUM_PIECES = 7;
	final public static int NUM_SIDES = 6;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Go through all the cases.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in original pieces in order.
			int[][] puzzle = new int[NUM_PIECES][NUM_SIDES];
			for (int i=0; i<NUM_PIECES; i++)
				for (int j=0; j<NUM_SIDES; j++)
					puzzle[i][j] = stdin.nextInt();

			// Solve.
			int[] ans = solve(puzzle);

			// Output result.
			if (ans == null)
				System.out.println("Case "+loop+": No solution");
			else {
				System.out.print("Case "+loop+":");
				for (int i=0; i<NUM_PIECES; i++)
					System.out.print(" "+ans[i]);
				System.out.println();
			}
		}
	}

	// Wrapper function.
	public static int[] solve(int[][] puzzle) {
		int[] perm = new int[NUM_PIECES];
		boolean[] used = new boolean[NUM_PIECES];
		return solveRec(puzzle, perm, used, 0);
	}

	public static int[] solveRec(int[][] puzzle, int[] perm, boolean[] used, int k) {

		// We've finished a position, return our answer.
		if (k == NUM_PIECES) {
			if (evaluate(puzzle, perm))
				return perm;
			else
				return null;
		}

		// Try each unused piece next.
		for (int i=0; i<NUM_PIECES; i++) {
			if (!used[i]) {
				used[i] = true;
				perm[k] = i;

				// If this branch leads to a solution, stop and return!!!
				int[] temp = solveRec(puzzle, perm, used, k+1);
				if (temp != null) return temp;

				// Move on...
				used[i] = false;
			}
		}

		// If we get here, nothing in this branch worked.
		return null;
	}

	public static boolean evaluate(int[][] puzzle, int[] perm) {

		// Easier if we copy over these references, for our sanity =)
		int[][] cur = new int[NUM_PIECES][];
		for (int i=0; i<NUM_PIECES; i++)
			cur[i] = puzzle[perm[i]];

		// Fix middle piece.
		rotate(cur[0], 0, 1);

		// Now, fix the rest of the pieces, according to the middle piece only.
		for (int i=1; i<NUM_PIECES; i++)
			rotate(cur[i], (i+2)%NUM_SIDES, cur[0][i-1]);


		for (int i=1; i<NUM_PIECES; i++) {

			// Set piece to compare piece i.
			int next = i+1;
			if (next >= NUM_PIECES) next = 1;

			// Check sides that are supposed to match...
			if (cur[i][(i+1)%NUM_SIDES] != cur[next][(i+4)%NUM_SIDES])
				return false;
		}

		return true;
	}

	// Rotates piece so that number fits in the location posMatch. (0 is top, 1 is top right, etc.)
	public static void rotate(int[] piece, int posMatch, int number) {

		// Would normally be dangerous, but this should be fine given what we are testing.
		while (piece[posMatch] != number)
			shift(piece);
	}

	// Shifts this piece counter clockwise.
	public static void shift(int[] piece) {
		int temp = piece[0];
		for (int i=1; i<NUM_SIDES; i++)
			piece[i-1] = piece[i];
		piece[NUM_SIDES-1] = temp;
	}
}