#include <iostream>
#include <vector>
#include <map>
using namespace std;

void erasec(vector<int>& c,int i,int j){
	int a,b;
	if(i>j){
		a=i,b=j;
	}else if(j>i){
		a=j,b=i;
	}else{
		return;
	}
	vector<int>::iterator it = c.begin();
	advance(it,a);
	it = c.erase(it);
	it = c.begin();
	advance(it,b);
	it = c.erase(it);
}

int solve(const vector<int>& c,map<vector<int>,int> &history){
	//vector<int> ex1;
	//vector<int> ex2;
	int min = (20>c.size())?c.size():20;
	for(int i=0;i<c.size();++i){
		for(int j=0;j<=1;++j){
			for(int k=-1;k<=1;++k){
				if(j==0&&k==0) continue;
				if(0<=i+j*4+k && i+j*4+k<c.size() && (i+j*4+k)%4-i%4<=1 && (i+j*4+k)%4-i%4>=-1){
					if(c[i] == c[i+j*4+k]){
						map<vector<int>,int>::iterator it = history.find(c);
						if(it != history.end()){
							if(it->second < min) min = it->second;
							continue;
						}else{
							vector<int> v(c.size());
							copy(c.begin(),c.end(),v.begin());
							erasec(v,i,i+j*4+k);
							int a = solve(v,history);
							history.insert( map<vector<int>, int>::value_type(v,a));
							if(a < min) min = a;
						}
					}
				}
			}
		}
	}
	return min;
}

int main(void){
	int n;
	cin >> n;
	for(int a=0;a<n;++a){
		vector<int> in;
		map<vector<int>,int> history;
		for(int i=0;i<20;++i){
			int input;
			cin >> input;
			in.push_back(input);
		}
		cout << solve(in,history) << "\n";
	}
	return 0;
}