import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt(), index = count;
while (count-- > 0) {
int n = sc.nextInt(), len = n * n;
boolean isCheck = true;
int[][] nums = new int[len + 1][len + 1];
boolean[] flag;
for (int i = 1; i <= len; i++) for (int j = 1; j <= len; j++) nums[i][j] = sc.nextInt();
for (int i = 1; i <= len && isCheck; i++) {//检查行
flag = new boolean[len + 1];
for (int j = 1; j <= len && isCheck; j++) {
if (nums[i][j] > len || flag[nums[i][j]]) isCheck = false;
else flag[nums[i][j]] = true;
}
}
for (int i = 1; i <= len && isCheck; i++) {//检查列
flag = new boolean[len + 1];
for (int j = 1; j <= len && isCheck; j++) {
if (flag[nums[j][i]]) isCheck = false;
flag[nums[j][i]] = true;
}
}
for (int i = 1; i <= n && isCheck; i++) //检查小方块
for (int j = 1; j <= n && isCheck; j++) {
flag = new boolean[len + 1];
for (int p = i * (n - 1) + 1; p <= i * n && isCheck; p++)
for (int q = j * (n - 1) + 1; q <= j * n && isCheck; q++) {
if (flag[nums[p][q]]) isCheck = false;
flag[nums[p][q]] = true;
}
}
String s = isCheck ? "Yes" : "No";
System.out.println("Case #" + (index - count) + ": " + s);
}
}
}