using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace Test1 {

    class ClassMain {



           
        public static void Main(String[] args) {
            string line;
            while ((line = Console.ReadLine()) != null) {
                int n = int.Parse(line);
                int[] puzzle = new int[31];

                for (int i = 0; i < n; i++) {
                    string[] strs = Console.ReadLine().Split(' ');

                    //load puzzle.
                    for (int j = 1; j <= 30; j++) {
                        puzzle[j] = int.Parse(strs[j-1]);
                    }
                    int solvedDepth = -1;
                    for (int dep = 0; dep <= 8; dep++) {
                        if (IDFS_AUX(puzzle, dep, 0, -1)) { solvedDepth = dep; break; }
                    }
                    Console.WriteLine(solvedDepth);


                }
                break;




            }
        }
        public static bool IDFS_AUX(int[] puzzle, int depth, int currentDepth, int beforeMv) {
            if (solve(puzzle)) { return true; }
            if (depth <= currentDepth) { return false; }
            for (int i = 1; i <= 4; i++) {
                if (i == beforeMv) { continue; }
                int[] rotated = rotPuzzle(puzzle,i);
                if(IDFS_AUX(rotated,depth,currentDepth+1,i)){
                    //Console.WriteLine(i+" at depth"+currentDepth);
                    //Console.WriteLine(string.Join(" ", rotated));
                    return true;
                }
            }
            return false;
        }




        public static bool solve(int[] puzzle) {
            int _1 = puzzle[1];
            for (int i = 2; i <= 9; i++) {
                if (puzzle[i] != _1) { return false; }
            }
            int _10 = puzzle[10];
            if (puzzle[11] != _10 || puzzle[12] != _10) { return false; }
            return true;
        }


        public static int[] rotPuzzle(int[] puzzle, int dir) {
            int[] rotated = new int[31];
            if (dir == 1) {
                rotated[7] = puzzle[22]; rotated[8] = puzzle[23];
                rotated[9] = puzzle[24];
                rotated[13] = puzzle[18]; rotated[18] = puzzle[13];
                rotated[22] = puzzle[7]; rotated[23] = puzzle[8];
                rotated[24] = puzzle[9];
                rotated[10] = puzzle[12]; rotated[12] = puzzle[10];
            }
            else if (dir == 2) {
                rotated[1] = puzzle[28]; rotated[2] = puzzle[29];
                rotated[3] = puzzle[30];
                rotated[21] = puzzle[19]; rotated[19] = puzzle[21];
                rotated[28] = puzzle[1]; rotated[29] = puzzle[2];
                rotated[30] = puzzle[3];
                rotated[16] = puzzle[15]; rotated[15] = puzzle[16];
            }
            else if (dir == 3) {
                rotated[1] = puzzle[24]; rotated[4] = puzzle[27];
                rotated[7] = puzzle[30];
                rotated[10] = puzzle[21]; rotated[21] = puzzle[10];
                rotated[24] = puzzle[1]; rotated[27] = puzzle[4];
                rotated[30] = puzzle[7];
                rotated[16] = puzzle[18]; rotated[18] = puzzle[16];
            }
            else if (dir == 4) {
                rotated[3] = puzzle[22]; rotated[6] = puzzle[25];
                rotated[9] = puzzle[28];
                rotated[19] = puzzle[12]; rotated[12] = puzzle[19];
                rotated[22] = puzzle[3]; rotated[25] = puzzle[6];
                rotated[28] = puzzle[9];
                rotated[13] = puzzle[15]; rotated[15] = puzzle[13];
            }
            for(int i =0; i<31; i++){
                if (rotated[i] == 0) { rotated[i] = puzzle[i]; }
            }
            return rotated;
        }


    }
}

Measured memory_bytes: 9872