// Arup Guha
// 5/19/2018
// Solution to 2018 Code Jam Round 2 Problem B Small: Costume Change

import java.util.*;

public class Solution {

	public static int n;
	public static int[][] grid;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();

		// Process each case.
		for (int loop=1; loop<=nC; loop++) {

			// Get the grid.
			n = stdin.nextInt();
			grid = new int[n][n];
			for (int i=0; i<n; i++)
				for (int j=0; j<n; j++)
					grid[i][j] = stdin.nextInt();

			int res = n*n;

			// i is mask of stuff we keep the same.
			for (int i=0; i<(1<<(n*n)); i++) {

				// If all the people represented by mask i can keep their costumes, 
				// see if we can do better than before
				if (okay(i))
					res = Math.min(res, n*n-Integer.bitCount(i));
			}
			System.out.println("Case #"+loop+": "+res);
		}
	}

	public static boolean okay(int mask) {

		// Extract out each person in this mask.
		ArrayList<Integer> bits = new ArrayList<Integer>();
		for (int i=0; i<n*n; i++)
			if ((mask & (1<<i)) != 0)
				bits.add(i);

		// For each pair, see if they are the same number and in either the same row or col
		// then we are screwed.
		for (int i=0; i<bits.size(); i++) {
			for (int j=i+1; j<bits.size(); j++) {
				int r1 = bits.get(i)/n;
				int c1 = bits.get(i)%n;
				int r2 = bits.get(j)/n;
				int c2 = bits.get(j)%n;
				if (r1 != r2 && c1 != c2) continue;
				if (grid[r1][c1] == grid[r2][c2]) return false;
			}
		}
		
		// If we get here, we are good.
		return true;
	}
}