#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;cin >> t;
    while(t--)
    {
        ll n; cin >> n;
        int mat[n][n];
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++) cin >> mat[i][j];
        int f = 0;
        for(int i=0;i<n;i++)
        {
            for(int j=1;j<n;j++)
            {
                if(mat[i][j]==mat[i][j-1])f=1;
                else if(i>1 and mat[i][j]==mat[i-1][j])f=1;
            }
        }
        if(f)cout << "No\n";
        else cout << "Yes\n";
    }
}