H, W = 5, 4
D = [
  [-1,-1], [0,-1], [1,-1],
  [-1,0],          [1,0],
  [-1,1],  [0,1],  [1,1],
]
Y, X = 0, 1
def dfs(card)
  return @memo[card] if @memo[card]
  result = card.flatten.size
  card.size.times do |i|
    card[i].size.times do |j|
      D.each do |d|
        y = i+d[Y]
        x = j+d[X]
        next if y<0 || card.size<=y
        next if x<0 || card[i].size<=x
        next if card[i][j] != card[y][x]
        c = card.map(&:dup)
        c[i][j] = c[y][x] = -1
        c[i].delete(-1)
        c[y].delete(-1)
        rearrange(c)
        result = [result, dfs(c)].min
      end
    end
  end
  @memo[card] = result
end

def rearrange(card)
  (card.size-1).times do |i|
    case card[i].size
      when W-1
        card[i].push(card[i+1].shift)
      when W-2
        card[i].push(*card[i+1].shift(2))
    end
  end
  card.delete([])
end

gets.to_i.times do
  c = Array.new
  H.times do
    c.push(gets.split.map(&:to_i))
  end
  @memo = Hash.new
  p dfs(c)
end