//dpoeschl import java.util.*; public class HexA { static Piece[] pieces = new Piece[7]; static Piece[] answer = new Piece[7]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); for(int c = 1; c <= n; c++) { for(int i = 0; i < 7; i++) pieces[i] = new Piece(i, scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()); System.out.print("Case " + c + ":"); if(!findAndPrintSolution(0)) System.out.println(" No solution"); } } public static boolean findAndPrintSolution(int position) { int centerToMatch, centerMatch, previousToMatch, previousMatch, nextToMatch, nextMatch; for(Piece piece : pieces) { if(!piece.used) { if(position <= 1) { piece.orientToPosition(1, position == 0 ? 0 : 3); answer[position] = piece; piece.used = true; if(findAndPrintSolution(position + 1)) return true; piece.used = false; } else if(position <= 5) { piece.orientToPosition(answer[0].valueAtPosition(position-1), (position + 2) % 6); if(answer[position-1].valueAtPosition(position) == piece.valueAtPosition((position + 3) % 6)) { answer[position] = piece; piece.used = true; if(findAndPrintSolution(position+1)) return true; piece.used = false; } } else { piece.orientToPosition(answer[0].valueAtPosition(5), 2); if(answer[5].valueAtPosition(0) == piece.valueAtPosition(3) && answer[1].valueAtPosition(4) == piece.valueAtPosition(1)) { answer[6] = piece; for(int i = 0; i < 7; i++) System.out.print(" " + answer[i].pieceNum); System.out.println(); return true; } } } } return false; } public static class Piece { public int pieceNum; public boolean used; private int[] arr; private int orientation; public Piece(int pieceNum, int a, int b, int c, int d, int e, int f) { this.pieceNum = pieceNum; arr = new int[] {a, b, c, d, e, f}; } public int valueAtPosition(int x) { x -= orientation; while(x < 0) x += 6; return arr[x % 6]; } public void orientToPosition(int value, int position) { for(int i = 0; i < 6; i++) if(arr[i] == value) orientation = position - i; } } }