#include <iostream>
#include <map>

using namespace std;

const int W = 4, H = 5;
int ans;
map<string, bool> memo;

int next(int pos, int dy, int dx)
{
    if (dy == 0 && dx == 0)
        return -1;
    int x = (pos % W) + dx, y = (pos / W) + dy;
    if (x < 0 || y < 0 || x >= W || y >= H)
        return -1;
    return y * W + x;
}

void Rec(string s)
{
    if (memo.end() != memo.find(s))
        return ;
    ans = min(ans, (int)s.size());
    memo.insert(map<string, int>::value_type(s, true));

    for (int i = 0; i < s.size(); ++i) {
        for (int dy = -1; dy <= 1; ++dy) {
            for (int dx = -1; dx <= 1; ++dx) {
                int nx = next(i, dy, dx);
                if (nx == -1 || nx >= s.size())
                    continue;
                if (s[i] != s[nx])
                    continue;

                string tmp;
                for (int l = 0; l < s.size(); ++l)
                    if (l != i && l != nx)
                        tmp.push_back(s[l]);

                Rec(tmp);
            }
        }
    }
}

int main()
{
    int N;

    cin >> N;
    while (N--) {
        // Input
        char ch;
        string card;

        for (int i = 0; i < H * W; ++i) {
            cin >> ch;
            card += ch;
        }
        // Solve
        ans = H * W;
        memo.clear();

        Rec(card);
        cout << ans << endl;
    }

    return 0;
}