import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the solve function below.
    static String solve(List<List<Integer>> board) {
        for (int i = 0; i < board.size() - 1; i++) {
            for (int j = 0; j < board.get(i).size() - 1; j++) {
                if (board.get(i).get(j) == board.get(i).get(j + 1)) return "No";
                if (board.get(i).get(j) == board.get(i + 1).get(j)) return "No";
            }
        }
        return "Yes";
    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());

        for (int tItr = 0; tItr < t; tItr++) {
            int n = Integer.parseInt(bufferedReader.readLine().trim());

            List<List<Integer>> board = new ArrayList<>();

            for (int i = 0; i < n; i++) {
                String[] boardRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

                List<Integer> boardRowItems = new ArrayList<>();

                for (int j = 0; j < n; j++) {
                    int boardItem = Integer.parseInt(boardRowTempItems[j]);
                    boardRowItems.add(boardItem);
                }

                board.add(boardRowItems);
            }

            String result = solve(board);

            bufferedWriter.write(result);
            bufferedWriter.newLine();
        }

        bufferedReader.close();
        bufferedWriter.close();
    }
}