#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <deque>

#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, m, n) for(int i = m; i < n; ++i)
#define rrep(i,n) for(int i = n; i >= 0; --i)
#define RREP(i, m, n) for(int i = n; i >= m; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;
typedef std::pair<long long, long long> pll;

using namespace std;

const long long mod = 1000000007;

int solve(vector<int> puzzle, vector<int> d, int num) {
	set<int> s = { 1, 2, 3, 4 };
	int size = d.size();
	if (size == 1) {
		s.erase(d[0]);
	}
	else if (size >= 2) {
		s.erase(d[size - 1]);
		if ((d[size - 1] == 1 && d[size - 2] == 2) || (d[size - 1] == 2 && d[size - 2] == 1) || (d[size - 1] == 3 && d[size - 2] == 4) || (d[size - 1] == 4 && d[size - 2] == 3)) {
			s.erase(d[size - 2]);
		}
	}

	int ans = 9999;
	for (auto i = s.begin(); i != s.end(); ++i) {
		if (i != s.begin()) {
			auto t = d.end();
			d.erase(--t);

			--i;
			switch (*i) {
			case 1:
				swap(puzzle[0], puzzle[27]);
				swap(puzzle[1], puzzle[28]);
				swap(puzzle[2], puzzle[29]);
				swap(puzzle[14], puzzle[15]);
				swap(puzzle[18], puzzle[20]);
				break;
			case 2:
				swap(puzzle[6], puzzle[21]);
				swap(puzzle[7], puzzle[22]);
				swap(puzzle[8], puzzle[23]);
				swap(puzzle[12], puzzle[17]);
				swap(puzzle[9], puzzle[11]);
				break;
			case 3:
				swap(puzzle[0], puzzle[23]);
				swap(puzzle[3], puzzle[26]);
				swap(puzzle[6], puzzle[29]);
				swap(puzzle[9], puzzle[20]);
				swap(puzzle[15], puzzle[17]);
				break;
			case 4:
				swap(puzzle[2], puzzle[21]);
				swap(puzzle[5], puzzle[24]);
				swap(puzzle[8], puzzle[27]);
				swap(puzzle[11], puzzle[18]);
				swap(puzzle[12], puzzle[14]);
				break;
			}
			++i;
		}
		d.push_back(*i);

		switch (*i) {
		case 1:
			swap(puzzle[0], puzzle[27]);
			swap(puzzle[1], puzzle[28]);
			swap(puzzle[2], puzzle[29]);
			swap(puzzle[14], puzzle[15]);
			swap(puzzle[18], puzzle[20]);
			break;
		case 2:
			swap(puzzle[6], puzzle[21]);
			swap(puzzle[7], puzzle[22]);
			swap(puzzle[8], puzzle[23]);
			swap(puzzle[12], puzzle[17]);
			swap(puzzle[9], puzzle[11]);
			break;
		case 3:
			swap(puzzle[0], puzzle[23]);
			swap(puzzle[3], puzzle[26]);
			swap(puzzle[6], puzzle[29]);
			swap(puzzle[9], puzzle[20]);
			swap(puzzle[15], puzzle[17]);
			break;
		case 4:
			swap(puzzle[2], puzzle[21]);
			swap(puzzle[5], puzzle[24]);
			swap(puzzle[8], puzzle[27]);
			swap(puzzle[11], puzzle[18]);
			swap(puzzle[12], puzzle[14]);
			break;
		}

		bool check = true;
		rep(j, 30) {
			int p;
			if (j < 9) p = 1;
			else if (j >= 9 && j < 12) p = 2;
			else if (j >= 12 && j < 15) p = 4;
			else if (j >= 15 && j < 18) p = 6;
			else if (j >= 18 && j < 21) p = 5;
			else p = 3;

			if (puzzle[j] != p) {
				check = false;
				break;
			}
		}

		if (check) return num + 1;
		else if (num + 1 < 8) ans = min(ans, solve(puzzle, d, num + 1));
	}

	return ans;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vector<int> puzzle(30);
	while (n--) {
		rep(i, 30) cin >> puzzle[i];
		vector<int> d;

		bool check = true;
		rep(j, 30) {
			int p;
			if (j < 9) p = 1;
			else if (j >= 9 && j < 12) p = 2;
			else if (j >= 12 && j < 15) p = 4;
			else if (j >= 15 && j < 18) p = 6;
			else if (j >= 18 && j < 21) p = 5;
			else p = 3;

			if (puzzle[j] != p) {
				check = false;
				break;
			}
		}

		if (check) cout << 0 << "\n";
		else cout << solve(puzzle, d, 0) << "\n";
	}

	return 0;
}


Measured memory_bytes: 3180