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

#define int long long
#define ll long long
#define rep(i,n) for (int i = 0; i < (n); i++)  
#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)
#define rap(i,a,n) for (int i = a; i < (n); i++) 
#define rrap(i,n,a) for (int i = (n)-1; i >= a; i--) 
#define LL_MAX 9223372036854775807

typedef vector<int> vi;  
typedef pair<int,int> ii;  
typedef vector<ii> vii;  
typedef list<int> li;  
typedef unordered_map<int,int> mii;

vector<vi> v1,v2;

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    rep(tt,t) {
        int n;
        cin >> n;

        v1.assign(n,vi());
        v2.assign(n,vi());

        rep(i,n) {
            string s;
            cin >> s;
            rep(j,n) {
                v1[i].push_back(s[j]-48);
            }
        }

        rep(i,n) {
            string s;
            cin >> s;
            rep(j,n) {
                v2[i].push_back(s[j]-48);
            }
        }

        rep(i,n) {
            if (v1[0][i] == v2[0][i]) continue;
            rep(j,n) {
                if (v1[j][i] == 0) v1[j][i] = 1;
                else v1[j][i] = 0;
            }
        }
        
        rap(i,1,n) {
            if (v1[i][0] != v2[i][0]) {
                rep(j,n) {
                    if (v1[i][j] == 0) v1[i][j] = 1;
                    else v1[i][j] = 0;
                }
            }
        }

        bool flag=false;
        rep(i,n) {
            rep(j,n) {
                if (v1[i][j] != v2[i][j]) {
                    flag=true;
                    break;
                }
            }
            if (flag) break;
        }

        if (flag) cout << "NO\n";
        else cout << "YES\n";
    }

    return 0;
}