# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 };
const char dir[4] = { 'u','l','d','r' };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
# pragma warning(disable:4996)

struct fcube {
	int p[31];
	int cnt;

	bool operator < (const fcube &f)const {
		for (int i = 1; i <= 30; i++) {
			return p[i] > f.p[i];
		}
		return false;
	}
};

bool istarget(fcube f) {
	bool boo[7] = {};
	int mem = f.p[1];
	boo[mem] = true;
	for (int i = 1; i <= 9; i++) {
		if (mem != f.p[i])return false;
	}
	mem = f.p[10];
	if (boo[mem])return false;
	boo[mem] = true;
	for (int i = 10; i <= 12; i++) {
		if (mem != f.p[i])return false;
	}
	mem = f.p[13];
	if (boo[mem])return false;
	boo[mem] = true;
	for (int i = 13; i <= 15; i++) {
		if (mem != f.p[i])return false;
	}
	mem = f.p[16];
	if (boo[mem])return false;
	boo[mem] = true;
	for (int i = 16; i <= 18; i++) {
		if (mem != f.p[i])return false;
	}
	mem = f.p[19];
	if (boo[mem])return false;
	boo[mem] = true;
	for (int i = 19; i <= 21; i++) {
		if (mem != f.p[i])return false;
	}
	mem = f.p[22];
	if (boo[mem])return false;
	boo[mem] = true;
	for (int i = 22; i <= 30; i++) {
		if (mem != f.p[i])return false;
	}
	return true;
}

fcube calc(fcube v, int i) {
	if (i == 0) {
		swap(v.p[7], v.p[22]);
		swap(v.p[8], v.p[23]);
		swap(v.p[9], v.p[24]);
		swap(v.p[13], v.p[18]);
		swap(v.p[10], v.p[12]);
	}
	else if (i == 1) {
		swap(v.p[1], v.p[28]);
		swap(v.p[2], v.p[29]);
		swap(v.p[3], v.p[30]);
		swap(v.p[15], v.p[16]);
		swap(v.p[19], v.p[21]);
	}
	else if (i == 2) {
		swap(v.p[1], v.p[24]);
		swap(v.p[4], v.p[27]);
		swap(v.p[7], v.p[30]);
		swap(v.p[10], v.p[21]);
		swap(v.p[16], v.p[18]);
	}
	else {
		swap(v.p[3], v.p[22]);
		swap(v.p[6], v.p[25]);
		swap(v.p[9], v.p[28]);
		swap(v.p[12], v.p[19]);
		swap(v.p[13], v.p[15]);
	}
	return v;
}

int bfs(fcube f,int cnt) {
	if (istarget(f))return cnt;
	if (cnt >= 8)return INF<int>();
	int res = INF<int>();
	REP(i, 4) {
		f=calc(f,i);
		res = min(res, bfs(f, cnt + 1));
		f=calc(f, i);
	}
	return res;
}

int main() {
	int n;
	cin >> n;
	REP(i, n) {
		fcube f;
		for (int j = 1; j <= 30; j++)cin >> f.p[j];
		cout << bfs(f,0) << endl;
	}
}


Measured memory_bytes: 3100