#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
using namespace std;
using ll = long long;

// 2-Sat solver
// Negated x is represented by ~x
struct TwoSat
{
    int N;
    vector<vector<int>> gr;
    vector<int> values; // 0 = false, 1 = true
    TwoSat(int n = 0) : N(n), gr(2*n) {}
    void either(int f, int j)
    {
        f = max(2*f, -1-2*f);
        j = max(2*j, -1-2*j);
        gr[f].push_back(j^1);
        gr[j].push_back(f^1);
    }
    vector<int> val, comp, z; int time = 0;
    int dfs(int i)
    {
        int low = val[i] = ++time, x; z.push_back(i);
        for (auto &e : gr[i])
            if (!comp[e])
                low = min(low, val[e] ?: dfs(e));
        if (low == val[i]) do
        {
            x = z.back(); z.pop_back();
            comp[x] = low;
            if (values[x>>1] == -1)
                values[x>>1] = x&1;
        } while (x != i);
        return val[i] = low;
    }
    bool solve()
    {
        values.assign(N, -1);
        val.assign(2*N, 0); comp = val;
        for (int i = 0; i < 2*N; ++i) if (!comp[i]) dfs(i);
        for (int i = 0; i < N; ++i) if (comp[2*i] == comp[2*i+1]) return 0;
        return 1;
    }
};
int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<string> v(n);
        for (auto &s : v)
            cin >> s;
        TwoSat T(2*n);
        for (int i = 0; i < n; ++i) {
            string s; cin >> s;
            for (int j = 0; j < n; ++j) {
                if (s[j] == v[i][j]) {
                    T.either(~i, (n+j));
                    T.either(i, ~(n+j));
                }
                else {
                    T.either(~i, ~(n+j));
                    T.either(i, n+j);
                }
            }
        }
        auto res = T.solve();
        if (res) cout << "YES\n";
        else cout << "NO\n";
    }
}