#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define pb push_back
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef pair<int, int> P;
typedef __int128_t int128;
const int maxn = 1e3 + 10;
const ll mod = 1e9 + 7;
char a[maxn][maxn];
char b[maxn][maxn];
bitset<maxn> bit[maxn];
bitset<maxn> bit1;
int n;
int main()
{
    IOS;
    int t;
    cin >> t;
    bit1.set();
    while (t--)
    {
        cin>>n;
        for (int i = 1; i <= n;i++)
            cin >> a[i] + 1;
        for (int i = 1; i <= n;i++)
            cin >> b[i] + 1;
        for (int i = 1; i <= n;i++)
        {
            for (int j = 1; j <= n;j++)
            {
                bit[i][j] = (a[i][j] - '0') ^ (b[i][j] - '0');
            }
        }
        for (int i = 1; i <= n;i++)
        {
            if(bit[i][1])
                bit[i] ^= bit1;
        }
        bool f = 1;
        for (int i = 1; i <= n;i++)
        {
            int tmp1 = bit[1][i];
            for (int j = 1;j<=n;j++)
            {
                if(bit[j][i]!=tmp1)
                    f = 0;
                if(!f)
                    break;
            }
            if(!f)
                break;
        }
        cout << (f ? "YES" : "NO") << endl;
    }
    return 0;
}
 	   		  	   			  	 	  	 	 		 	