#include<bits/stdc++.h>;
#define mod 1000000007
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long
#define f(a,b) for(int i=a; i<b; i++)
#define rep(i,a,b) for(int i=a;i< b;i++)
#define endl "\n"
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define fr first
#define sc second
#define INF 1000000000
using namespace std;
void solve()
{
    int n;
    cin>>n;
    string a[n] , b[n];
    rep(i,0,n) cin>>a[i];
    rep(i,0,n) cin>>b[i];

    rep(i,0,n)
    {
        if(a[0][i]!=b[0][i]){
            rep(j,0,n)
            {
                a[j][i]^=1;
            }
        }
    }

    rep(i,1,n)
    {
        if(a[i]!=b[i]){
            rep(j,0,n)
            {
                a[i][j]^=1;
            }
            if(a[i]!=b[i])
           {
               cout<< "NO\n";
               return;
           }
        }

    }
    cout<< "YES\n";

}
int main()
{
     fast_io;
     int t=1;
     cin>>t;
     while(t--) {
             solve();
     }
return 0;
}


