#include<iostream>
#include<queue>
#include<vector>
using namespace std;
//queue?????\??£???????????????????????????pop?????????,??´??¢????????¢?????????
queue<std::vector<int> > cubes;
vector<int> puzzle;
vector<int> tmppuzzle;
namespace my{
int top,left,right,up,down,back;
}

void clear(){
  for(int i=0;i<32;i++){
    tmppuzzle[i] = puzzle[i];
  }
}

int  BFS(){

  while(!cubes.empty()){
    cubes.pop();
  }

  bool flag = true;
  puzzle[30] = puzzle[31] = 0;
  cubes.push(puzzle);

  while(1){

    flag = true;
    puzzle = cubes.front();
    cubes.pop();
    if(puzzle[30] == 8){
      return 8;
    }
    my::top = puzzle[0];
    my::left = puzzle[15];
    my::right = puzzle[12];
    my::up = puzzle[18];
    my::down = puzzle[9];
    my::back = puzzle[21];


    if(flag){
      for(int i=1;i<9;i++)
        if(my::top != puzzle[i]){
          flag = false;
          break;
        }
    }

    if(flag){
      for(int i=16;i<18;i++){
        if(my::left != puzzle[i]){
          flag = false;
          break;
        }
      }
    }

    if(flag){
      for(int i=13;i<15;i++){
        if(my::right != puzzle[i]){
          flag = false;
          break;
        }
      }
    }

    if(flag){
      for(int i=19;i<21;i++){
        if(my::up != puzzle[i]){
          flag = false;
          break;
        }
      }
    }

    if(flag){
      for(int i=9;i<12;i++){
        if(my::down != puzzle[i]){
          flag = false;
          break;
        }
      }
    }

    if(flag){
      for(int i=22;i<30;i++){
        if(my::back != puzzle[i]){
          flag = false;
          break;
        }
      }
    }

    if(flag){
      return puzzle[30];
    }


    puzzle[30] += 1;
    if(tmppuzzle[31] != 1){
      clear();
      swap(tmppuzzle[17],tmppuzzle[12]);
      swap(tmppuzzle[7],tmppuzzle[22]);
      swap(tmppuzzle[6],tmppuzzle[21]);
      swap(tmppuzzle[8],tmppuzzle[23]);
      swap(tmppuzzle[9],tmppuzzle[11]);//??´??¢
      tmppuzzle[31] = 1;
      cubes.push(tmppuzzle);
    }

    if(tmppuzzle[31] != 2){
      clear();
      swap(tmppuzzle[0],tmppuzzle[27]);
      swap(tmppuzzle[1],tmppuzzle[28]);
      swap(tmppuzzle[2],tmppuzzle[29]);
      swap(tmppuzzle[14],tmppuzzle[15]);
      swap(tmppuzzle[18],tmppuzzle[20]);//??´??¢
      tmppuzzle[31] = 2;
      cubes.push(tmppuzzle);
    }

    if(tmppuzzle[31] != 3){
      clear();
      swap(tmppuzzle[0],tmppuzzle[23]);
      swap(tmppuzzle[3],tmppuzzle[26]);
      swap(tmppuzzle[6],tmppuzzle[29]);
      swap(tmppuzzle[9],tmppuzzle[20]);
      swap(tmppuzzle[15],tmppuzzle[17]);//??´??¢
      tmppuzzle[31] = 3;
      cubes.push(tmppuzzle);
    }

    if(tmppuzzle[31] != 4){
      clear();
      swap(tmppuzzle[2],tmppuzzle[21]);
      swap(tmppuzzle[5],tmppuzzle[24]);
      swap(tmppuzzle[8],tmppuzzle[27]);
      swap(tmppuzzle[11],tmppuzzle[18]);
      swap(tmppuzzle[12],tmppuzzle[14]);//??´??¢
      tmppuzzle[31] = 4;
      cubes.push(tmppuzzle);
    }
  }

  return -1;
}

int main(){

  int n;
  cin >> n;

  puzzle.resize(32);
  tmppuzzle.resize(32);

  for(int i=0;i<n;i++){
    for(int i=0;i<30;i++){
      cin >> puzzle[i];
    }
    cout << BFS() << endl;
  }

  return 0;
}

Measured memory_bytes: 13664