#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <math.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

#if __cplusplus >= 201103L
    #include <unordered_set>
    #include <unordered_map>
    #include <chrono>
    #include <random>
    #include <bits/stdc++.h>
    struct pairhash {
        template<class T, class U>
        size_t operator() (const pair<T,U> &p) const {
            static const uint64_t FIXED_RANDOM = (uint64_t)chrono::steady_clock::now().time_since_epoch().count();
            return splitmix64(p.first + FIXED_RANDOM) ^ splitmix64(p.second+ FIXED_RANDOM);
        }
        static uint64_t splitmix64(uint64_t x) {
            x += 0x9e3779b97f4a7c15;
            x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
            x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
            return x ^ (x >> 31);
        }
    };
    mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

    inline int randint(int l, int r) {
        return uniform_int_distribution<int>(l,r)(rng);
    }

    inline double randdouble(double l, double r) {
        return uniform_real_distribution<double>(l,r)(rng);
    }
#endif
 

#define fastio ios::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define ll128 __int128_t
#define PI 3.14159265358979323846
#define min(a,b) ((a<b)?(a):(b))
#define max(a,b) ((a<b)?(b):(a))
#define abs(a) ((a>0)?a:-(a))
#define pii pair<int,int>
#define pll pair<ll,ll>


template <typename T>
inline void read(T& x) {
    x = 0; 
    bool f = 0;
    char ch = (char)getchar();
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') f = 1;        // 判断是否为负
        ch = (char)getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x*10 + (ch&15);  // 将新读入的数字’加’在 x 的后面, ch&15等同于ch-'0'
        ch = (char)getchar();  // 继续读入
    }
    if (f) x = -x;
}

template <typename T>
inline void write(T x) {
    static int st[35];
    int top = 0;
    do {
        st[top++] = x % 10; x /= 10;
    } while (x);
    while (top) putchar(st[--top] + '0');
}

const double pi = acos(-1.0);
const double eps = (double)1e-4;

const int mod = 1e9+7;
const int maxn = 1e3+5;
const int maxm = 2e5+10;

int arr[maxn][maxn];
int tmp[maxn][maxn];
int n;

void flip_row(int i) {
    for (int j = 1; j <= n; j++) arr[i][j] ^= 1;
}

void flip_col(int j) {
    for (int i = 1; i <= n; i++) arr[i][j] ^= 1;
}

bool check1() {
    
    memcpy(tmp, arr, sizeof(arr));

    flip_row(1);
    for (int j = 1; j <= n; j++) {
        if (arr[1][j]) flip_col(j);
    }
    
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j]) {
                flip_row(i);
                break;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j]) return 0;
        }
    }

    return 1;
}

bool check2() {
    memcpy(arr, tmp, sizeof(arr));

    for (int j = 1; j <= n; j++) {
        if (arr[1][j]) flip_col(j);
    }
    
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j]) {
                flip_row(i);
                break;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j]) return 0;
        }
    }

    return 1;
}

int main() {
    fastio;

    int T; cin >> T;
    while (T--) {
        string s;
        char c;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> s;
            for (int j = 1; j <= n; j++) {
                c = s[j-1];
                if (c == '1') arr[i][j] = 1;
                else arr[i][j] = 0;
            }
        }

        for (int i = 1; i <= n; i++) {
            cin >> s;
            for (int j = 1; j <= n; j++) {
                c = s[j-1];
                if (c == '1') arr[i][j] ^= 1;
            }
        }
        if (check1() || check2()) {
            cout << "YES" << "\n";
        } else cout << "NO" << "\n";
    }
}