
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<stdio.h>
#include<map>
#include<queue>
using namespace std;
#define Check(x) cout<<"x:"<<x<<" "
#define Min(x,y,z) min(x,min(z,y))
#define Max(x,y,z) max(x,max(z,y))
typedef long long ll;
const int MAXN = 1e3 + 5;
const int MAXM = 1e6 + 5;
const ll mod = 1e9 + 7;
ll n, m;
int mp[MAXN][MAXN];
int ver[MAXN];
int hor[MAXN];
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= n;j++) {
                char ch;
                cin >> ch;
                mp[i][j] = ch - '0';
            }
        }
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= n;j++) {
                char ch;
                cin >> ch;
                mp[i][j] = (mp[i][j] + (ch - '0')) % 2;
            }
        }
        if (n == 1) {
            cout << "YES" << endl;
            continue;
        }
        bool f = true;
        for (int i = 1;i <= n;i++) {
            if (mp[1][i] == 1) {
                hor[i] = 1;
            }
        }
        for (int i = 2;i <= n;i++) {
            if (hor[1])mp[i][1] = mp[i][1] ^ 1;
            if (mp[i][1] == 1)ver[i] = 1;
            else ver[i] = 0;
            for (int j = 2;j <= n;j++) {
                for (int k = 1;k <= ver[i] + hor[j];k++) {
                    mp[i][j] = mp[i][j] ^ 1;
                }
                if (mp[i][j] == 1) { f = false;break; }
            }
            if (!f)break;
        }
        for (int i = 1;i <= n;i++) {
            ver[i] = hor[i] = 0;
        }
        if (f)cout << "YES" << endl;
        else cout << "NO" << endl;
    }

}