#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

typedef pair<int, vi> Data;

bool check(vi& brd)
{
    for (int i = 0; i < 9; ++i) if (brd[i] != brd[0]) return false;
    for (int i = 9; i < 12; ++i) if (brd[i] != brd[9]) return false;
    for (int i = 12; i < 15; ++i) if (brd[i] != brd[12]) return false;
    for (int i = 15; i < 18; ++i) if (brd[i] != brd[15]) return false;
    for (int i = 18; i < 21; ++i) if (brd[i] != brd[18]) return false;
    for (int i = 21; i < 30; ++i) if (brd[i] != brd[21]) return false;

    return true;
}

void swap(vi& brd, int type)
{
    if (type == 0) {
        swap(brd[9 - 1], brd[24 - 1]);
        swap(brd[8 - 1], brd[23 - 1]);
        swap(brd[7 - 1], brd[22 - 1]);

        swap(brd[10 - 1], brd[12 - 1]);

        swap(brd[13 - 1], brd[18 - 1]);
    } else if (type == 1) {
        swap(brd[1 - 1], brd[28 - 1]);
        swap(brd[2 - 1], brd[29 - 1]);
        swap(brd[3 - 1], brd[30 - 1]);

        swap(brd[19 - 1], brd[21 - 1]);

        swap(brd[15 - 1], brd[16 - 1]);
    } else if (type == 2) {
        swap(brd[1 - 1], brd[24 - 1]);
        swap(brd[4 - 1], brd[27 - 1]);
        swap(brd[7 - 1], brd[30 - 1]);

        swap(brd[16 - 1], brd[18 - 1]);

        swap(brd[10 - 1], brd[21 - 1]);
    } else if (type == 3) {
        swap(brd[3 - 1], brd[22 - 1]);
        swap(brd[6 - 1], brd[25 - 1]);
        swap(brd[9 - 1], brd[28 - 1]);

        swap(brd[13 - 1], brd[15 - 1]);

        swap(brd[12 - 1], brd[19 - 1]);
    }
}

signed main()
{
    int N; cin >> N;

    while (N--) {
        vi pzl(30);
        rep(i, 30) cin >> pzl[i];

        queue<Data> q;
        q.push(Data(0, pzl));

        while (q.size()) {
            Data data = q.front();
            q.pop();

            int b = data.fst;

            vi state = data.scd;

            if (check(state)) {
                cout << b << endl;
                break;
            }

            if (b >= 8) continue;

            for (int i = 0; i < 4; ++i) {
                swap(state, i);

                q.push(Data(b + 1, state));

                swap(state, i);
            }
        }
    }
}

Measured memory_bytes: 11572