//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define forr(i,n) for(int i=0;i<n;i++)
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi first
#define se second
#define pb push_back
//#define mp make_pair
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define CASE(t) ll t; cin>>t; while(t--)
#define sor(v) sort(v.begin(),v.end())
#define rev(v) reverse(v.begin(),v.end())
#define vi vector<ll>
#define cs(ele) cout<<ele<<'\n'
#define pii pair<ll,ll>
#define MAXN   100001
const ll mod=1e9 +7;
//const ll mod=998244353;
ll Mod(ll n){return ((n%mod)+mod)%mod;}
//ll power(ll x,ll y){ll res=1;x=x;while(y>0){if(y&1)res=mod(res*x);x=mod(x*x);y=mod(y>>1);}return mod(res);}
void fast(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

}
ll f[200005];
ll invfact[200005];
ll power(ll x,ll y){
     ll res=1;
     while(y>0){
         if(y&1){
             res=(res*x)%mod;
         }
         x=(x*x)%mod;
         y>>=1;
    }
    return res;
}
void precal(){
    f[0]=1;
    invfact[0]=1;
    for(int i=1;i<200005;i++){
        f[i]=(f[i-1]*i)%mod;
        invfact[i]=power(f[i],mod-2);
    }
}
ll ncr(ll n,ll k){
    if(n<k||n<0||k<0){
        return 0;
    }
    return ((f[n]*invfact[k])%mod*invfact[n-k])%mod;
}

bool prime[1000011];
void primenumbers(ll n)
{

    memset(prime, true, sizeof(prime));

    for (ll p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (ll i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
ll modI(ll a, ll m)
{
    ll m0=m,y=0,x=1;
    if(m==1) return 0;
    while(a>1)
    {
        ll q=a/m;
        ll t=m;
        m=a%m;
        a=t;
        t=y;
        y=x-q*y;
        x=t;
    }
    if(x<0) x+=m0;
    return x;
}
int spf[MAXN];

void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)

        spf[i] = i;

    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {
            for (int j=i*i; j<MAXN; j+=i)

                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
vector<int> getFactorization(int x)
{
    vector<int> ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}
const ll mxn=400010;
ll st[mxn];
ll a[mxn];
void build(ll v,ll l,ll r){
  if(l==r){
    st[v]=a[l];
  }
  else{
    build(2*v,l,(l+r)/2);
    build(2*v+1,(l+r)/2+1,r);
    st[v]=min(st[2*v],st[2*v+1]);
  }
}
void update(ll v,ll l,ll r,ll pos,ll val){
  if(pos<l||pos>r){return ;}
  if(pos==l&&pos==r){
    st[v]=val;
  }
  else{
    update(2*v,l,(l+r)/2,pos,val);
    update(2*v+1,(l+r)/2+1,r,pos,val);
    st[v]=min(st[2*v],st[2*v+1]);
  }

}
ll query(ll v,ll l,ll r,ll tl,ll tr){
  if(tr<l||tl>r||tl>tr){return 1e10;}
  if(l==tl&&r==tr){
    return st[v];
  }
  else{
    return min(query(2*v,l,(l+r)/2,tl,min(tr,(l+r)/2)),query(2*v+1,(l+r)/2+1,r,max(tl,(l+r)/2+1),tr));

  }
}
const ll mx=2e5+10;
ll dp[mx];
ll cnt[mx];

int main()
{
  fast();
  CASE(t){
  ll n;
  cin>>n;
  string s[n],s1[n];
  f(i,0,n){cin>>s[i];}
  f(i,0,n){cin>>s1[i];}
  f(i,0,n){
    if(s[i][0]!=s1[i][0]){
        f(j,0,n){
          s[i][j]='1'+'0'-s[i][j];
        }
    }
  }
  f(i,0,n){
    if(s[0][i]!=s1[0][i]){
        f(j,0,n){
          s[j][i]='1'+'0'-s[j][i];
        }
    }
  }
  bool ok=true;
  f(i,0,n){
    ok=ok&(s[i]==s1[i]);
  }
  if(ok){cs("YES");}
  else{cs("NO");}

  }

  return 0;
}
