#include <iostream>
using namespace std;

int searchCorner(int a, int b, int c, int d, int e, int f);
bool isRightCorner(int a, int b, int c, int d);

class floppyCube {
private:
  bool cross[4];
  int corner[4];

public:
  floppyCube() {
    for(int i = 0; i < 4; i++) {
      cross[i] = true;
      corner[i] = i;
    }
  }

  void setCube(floppyCube c) {
    for(int i = 0; i < 4; i++) {
      cross[i] = c.getCross(i);
      corner[i] = c.getCorner(i);
    }
  }

  void setCube();
  void printCube();
  void process(int i);
  int solve(int count, int max, int prevProcess);
  bool isSolved();

  bool getCross(int i) { return cross[i]; }
  int getCorner(int i) { return corner[i]; }
};

int floppyCube::solve(int count, int max, int prevProcess){
  if(isSolved()) {
    return count;
  }
  if(count == max) {
    return count;
  }

  floppyCube temp(*this);
  if(count == 0) {
    temp.process(0);
    max = temp.solve(1, max, 0);

    temp.setCube(*this);
    temp.process(1);
    max = temp.solve(1, max, 1);

    temp.setCube(*this);
    temp.process(2);
    max = temp.solve(1, max, 2);

    temp.setCube(*this);
    temp.process(3);
    max = temp.solve(1, max, 3);
  } else {
    for(int i = 0; i < 4; i++) {
      if(prevProcess != i) {
        temp.setCube(*this);
        temp.process(i);
        max = temp.solve(count + 1, max, i);
      }
    }
  }

  return max;
}

int main(void){
  int num;
  cin >> num;

  int answer;
  floppyCube cube;
  for(int i = 0; i < num; i++) {
    cube.setCube();
    answer = cube.solve(0, 8, 0);
    cout << answer << endl;
  }
}

bool floppyCube::isSolved() {
  if(!(cross[0] && cross[1] && cross[2] && cross[3])) {
    return false;
  }

  for(int i = 0; i < 4; i++) {
    if(corner[i] != i) {
      return false;
    }
  }

  return true;
}

void floppyCube::process(int i) {
  cross[i] = !cross[i];

  int temp;
  switch(i) {
    case 0: temp = corner[0]; corner[0] = corner[1]; corner[1] = temp; break;
    case 1: temp = corner[1]; corner[1] = corner[2]; corner[2] = temp; break;
    case 2: temp = corner[2]; corner[2] = corner[3]; corner[3] = temp; break;
    case 3: temp = corner[3]; corner[3] = corner[0]; corner[0] = temp; break;
  }
}

void floppyCube::setCube() {
  int color[30];
  for(int i = 0; i < 30; i++) {
    cin >> color[i];
  }
  cross[0] = (color[4] == color[1]);
  cross[1] = (color[4] == color[5]);
  cross[2] = (color[4] == color[7]);
  cross[3] = (color[4] == color[3]);

  corner[0] = searchCorner(color[19], color[13], color[10], color[16], color[15], color[20]);
  corner[1] = searchCorner(color[19], color[13], color[10], color[16], color[18], color[14]);
  corner[2] = searchCorner(color[19], color[13], color[10], color[16], color[12], color[11]);
  corner[3] = searchCorner(color[19], color[13], color[10], color[16], color[9], color[17]);
}

int searchCorner(int a, int b, int c, int d, int e, int f) {
  if(isRightCorner(d, a, e, f)) {
    return 0;
  } else if(isRightCorner(a, b, e, f)) {
    return 1;
  } else if(isRightCorner(b, c, e, f)) {
    return 2;
  } else {
    return 3;
  }
}

bool isRightCorner(int a, int b, int c, int d) {
  return ((a == c && b == d) || (a == d && b == c));
}

Measured memory_bytes: 3076