#include<bits/stdc++.h>
using namespace std;
    #define author          "Sihare0riginals"
    #define ss              string 
    #define lli             long long int
    #define ld              long double
    #define f               first
    #define s               second
    #define all(v)          v.begin(),v.end()
    #define I(a,i)          memset(a,i,sizeof(a));
    #define pb              push_back
    #define mp              make_pair
    #define L(i,start,end)  for(lli i=start;i<=end;i++)
    #define R(i,start,end)  for(lli i=start;i>=end;i--)
    #define ip(n)           cin>>n;lli ip[n+1];L(i,1,n)cin>>ip[i];
const lli LINF = 1e18,mod=1e9+7;
void Bl0ck_M0mb0();
 
 
signed main()
{
    Bl0ck_M0mb0();
    lli t=1;
    cin>>t;
    while(t--){
        lli n;
        cin>>n;
        ss ip[n+1],rs[n+1];
        L(i,1,n)
        cin>>ip[i];
        L(i,1,n)
        cin>>rs[i];
        lli row[n+1];
        I(row,0);
        L(i,0,n-1)
        {
            lli x=0;
            if(ip[1][i]!=rs[1][i])
                row[i]=1;
        }
        // L(i,0,n-1)
        // cout<<row[i]<<" ";
        // cout<<endl;
        bool poss=true;
        L(i,2,n)
        {
            set<lli> x;
            L(j,0,n-1)
            {
                if(ip[i][j]!=rs[i][j])
                    x.insert(!row[j]);
                else
                    x.insert(row[j]);
            }
            if(x.size()>1)
            {
                poss=false;
                break;
            }
        }
        if(poss)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}





























void Bl0ck_M0mb0(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("ip.txt","r",stdin);
    freopen("op.txt","w",stdout);
    #endif
}
/*
#define dbg1D(i,start,end,arr) for(lli i=start;i<=end;i++) cout<<i<<"th : "<<arr[i]<<endl;
#define dbg2D(i,j,s1,e1,s2,e2,arr) for(lli i=s1;i<=e1;i++){cout<<i<<"th :";for(lli j=s2;j<=e2;j++)cout<<arr[i][j]<<" ";cout<<endl;}
#define M map<lli,lli> \n #define sz(s) s.size()-1
#define P pair<lli,lli> 
#define V vector<lli>
lli dx[4]={-1, 0, 0, +1};
lli dy[4]={0, -1, +1, 0};
lli power(lli a,lli b,lli m){ if(b==0) return 1; if(b==1) return a%m;
    lli t=power(a,b/2,m); t=(t*t)%m; if(b&1) t=(t*a)%m; return t;}
lli modInverse(lli a, lli m) { return power(a, m-2, m); } */