#include <bits/stdc++.h>
 
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>> a(n, vector<int>(n));
        for (int i = 0; i < n; i++) {
            for  (int j = 0; j < n; j++) {
                cin >> a[i][j];
            }
        }
        bool ok = true;
        for (int i = 0; i < n; i++) {
            for  (int j = 0; j < n; j++) {
                if (i+1 < n && a[i][j] == a[i+1][j]) {
                    ok = false;
                }
                if (j+1 < n && a[i][j] == a[i][j+1]) {
                    ok = false;
                }
            }
        }
        cout << (ok ? "Yes" : "No") << endl;
    }
}