#include<iostream>
using namespace std;


class Cube {
public:
	void setSurface(int surface[]) {
		for (int i = 0; i < 30; i++) {
			if (i < 9) {
				top[i] = surface[i];
			}
			else if (i < 12) {
				front[i - 9] = surface[i];
			}
			else if (i < 15) {
				right[i - 12] = surface[i];
			}
			else if (i < 18) {
				left[i - 15] = surface[i];
			}
			else if (i < 21) {
				rear[i - 18] = surface[i];
			}
			else {
				under[i - 21] = surface[i];
			}
		}
	}
	void spinFront() {
		int temp;
		for (int i = 0; i < 3; i++) {
			temp = top[i + 6];
			top[i + 6] = under[i];
			under[i] = temp;
		}
		temp = right[0];
		right[0] = left[2];
		left[2] = temp;
		
		temp = front[0];
		front[0] = front[2];
		front[2] = temp;
	}
	void spinRear() {
		int temp;
		for (int i = 0; i < 3; i++) {
			temp = top[i];
			top[i] = under[i + 6];
			under[i + 6] = temp;
		}
		temp = right[2];
		right[2] = left[0];
		left[0] = temp;

		temp = rear[0];
		rear[0] = rear[2];
		rear[2] = temp;
	}
	void spinRight() {
		int temp;
		for (int i = 2; i < 9; i += 3) {
			temp = top[i];
			top[i] = under[i - 2];
			under[i - 2] = temp;
		}
		temp = front[2];
		front[2] = rear[0];
		rear[0] = temp;

		temp = right[0];
		right[0] = right[2];
		right[2] = temp;
	}
	void spinLeft() {
		int temp;
		for (int i = 0; i < 9; i += 3) {
			temp = top[i];
			top[i] = under[i + 2];
			under[i + 2] = temp;
		}
		temp = front[0];
		front[0] = rear[2];
		rear[2] = temp;

		temp = left[0];
		left[0] = left[2];
		left[2] = temp;
	}
	bool checkFlush() {
		for (int i = 1; i < 9; i++) {
			if (top[0] != top[i]) {
				return false;
			}
			if (under[0] != under[i]) {
				return false;
			}
		}
		for (int i = 1; i < 3; i++) {
			if (front[0] != front[i]) {
				return false;
			}
			if (rear[0] != rear[i]) {
				return false;
			}
			if (right[0] != right[i]) {
				return false;
			}
			if (left[0] != left[i]) {
				return false;
			}
		}
		return true;
	}
	void operator = (const Cube &cube) {
		for (int i = 0; i < 9; i++) {
			this->top[i] = cube.top[i];
			this->under[i] = cube.under[i];
		}
		for (int i = 0; i < 3; i++) {
			this->front[i] = cube.front[i];
			this->rear[i] = cube.rear[i];
			this->right[i] = cube.right[i];
			this->left[i] = cube.left[i];
		}
	}
private:
	int top[9];
	int under[9];
	int front[3];
	int rear[3];
	int right[3];
	int left[3];
};



bool checkS(Cube cube, int depth) {
	Cube copy;
	
	if (depth <= 0) {
		return false;
	}
	depth--;

	copy = cube;
	copy.spinFront();
	if (copy.checkFlush()) {
		return true;
	}
	if (checkS(copy, depth)) {
		return true;
	}
	copy = cube;
	copy.spinLeft();
	if (copy.checkFlush()) {
		return true;
	}
	if (checkS(copy, depth)) {
		return true;
	}
	copy = cube;
	copy.spinRear();
	if (copy.checkFlush()) {
		return true;
	}
	if (checkS(copy, depth)) {
		return true;
	}
	copy = cube;
	copy.spinRight();
	if (copy.checkFlush()) {
		return true;
	}
	if (checkS(copy, depth)) {
		return true;
	}

	return false;
}

int checkSpin(Cube cube) {
	int depth = 1;

	if (cube.checkFlush()) {
		return 0;
	}

	while (!checkS(cube,depth)) {
		depth++;
	}
	return depth;
}


int main() {
	int N;
	int surface[30];
	Cube cube[30];

	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 30; j++) {
			cin >> surface[j];
		}
		cube[i].setSurface(surface);
	}

	for (int i = 0; i < N; i++) {
		cout << checkSpin(cube[i]) << endl;
	}

	return 0;
}

Measured memory_bytes: 1168