#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int card[20];

int solve(int m){
  int now[20];
  int cnt = 0;
  int toi[20];
  bool flag = true;

  REP(i,20) if((m & (1 << i)) != 0){
    now[cnt] = card[i];
    toi[cnt] = i;

    //printf("%d%c", card[i], (cnt % 4 == 3 ? '\n' : ' '));

    cnt++;
  }
  //if(cnt % 4 != 0) puts("");
  //puts("");

  int ans = cnt;

  REP(i,cnt){
    if(i % 4 != 3){
      if(i + 1 < cnt){
        if(now[i] == now[i+1]){
          int mm = m ^ (1 << toi[i]) ^ (1 << toi[i+1]);
          ans = min(ans, solve(mm));
        }
      }
    }
    if(i < 16){
      if(i + 4 < cnt){
        if(now[i] == now[i+4]){
          int mm = m ^ (1 << toi[i]) ^ (1 << toi[i+4]);
          ans = min(ans, solve(mm));
        }
      }
    }
    if(i % 4 != 3 && i < 16){
      if(i + 5 < cnt){
        if(now[i] == now[i+5]){
          int mm = m ^ (1 << toi[i]) ^ (1 << toi[i+5]);
          ans = min(ans, solve(mm));
        }
      }
    }
    if(i % 4 != 0 && i < 16){
      if(i + 3 < cnt){
        if(now[i] == now[i+3]){
          int mm = m ^ (1 << toi[i]) ^ (1 << toi[i+3]);
          ans = min(ans, solve(mm));
        }
      }
    }
  }

  return ans;
}

int main(){
  int cc = getInt();
  while(cc --> 0){
    REP(i,20) card[i] = getInt();
    print(solve((1<<20)-1));
  }
  return 0;
}