#!/bin/ruby

require 'json'
require 'stringio'

# Complete the solve function below.
def solve(board)
  l = board.length
  (0...l).all? {|r| (0...l).all? {|c|
    (r == 0 || board[r][c] != board[r - 1][c]) &&
    (c == 0 || board[r][c] != board[r][c - 1])
    }} ? "Yes" : "No"
end

fptr = File.open(ENV['OUTPUT_PATH'], 'w')

t = gets.strip.to_i

t.times do |t_itr|
    n = gets.strip.to_i

    board = Array.new(n)

    n.times do |i|
        board[i] = gets.rstrip.split.map(&:to_i)
    end

    result = solve board

    fptr.write result
    fptr.write "\n"
end

fptr.close()