#include<bits/stdc++.h>
using namespace std;
#define dd double
#define ll long long int 
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll mod=1e9+7;
bool func(vector<vector<char> > &v1,vector<vector<char> > &v2){
    vector<ll> rem(v1.size(),0),rem1(v1.size(),0);
    for (int i = 0; i < v1.size(); i++)
    {
       vector<char> temp1=v2[i];
       vector<char>temp2(v1.size());
       for(int j=0;j<v1.size();j++){
           if(rem[j]%2!=0){
              temp2[j]=(v1[i][j]=='1')?'0':'1';
           }
           else temp2[j]=v1[i][j];
       }
       if(temp1!=temp2){
          int t;
          for( t=0;t<temp1.size();t++){
            if(temp1[t]==temp2[t]) break;
          }
          if(t==temp1.size()) rem1[i]+=1;
          else{
             for(int t=0;t<temp1.size();t++){
                 if(temp1[t]!=temp2[t]) rem[t]+=1;
             }
          }
       }
    }
    for(int i=0;i<rem.size();i++){
       if(rem[i]%2!=0){
          for(int j=0;j<v1.size();j++){
              v1[j][i]=(v1[j][i]=='1')?'0':'1';
          }
       }
    }
    for(int i=0;i<rem1.size();i++){
      if(rem1[i]%2!=0){
         for(int j=0;j<v1[0].size();j++){
              v1[i][j]=(v1[i][j]=='1')?'0':'1';
         }
      }
    }
    return v1==v2;
}
int main()
{
  #ifndef ONLINE_JUDGE
  freopen("input1.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  fastio;
  ll test_case;
  test_case=1;
  cin>>test_case;
  while(test_case--){
       ll n;
       cin>>n;
       vector<vector<char> >v1(n,vector<char>(n)),v2(n,vector<char>(n));
       for (int i = 0; i < n; ++i)
       {
        for (int j = 0; j < n; ++j)
        {
            cin>>v1[i][j];
        }
       }
       for (int i = 0; i < n; ++i)
       {
        for (int j = 0; j < n; ++j)
        {
            cin>>v2[i][j];
        }
       }
       bool ans=func(v1,v2);
       (ans)?cout<<"YES"<<endl:cout<<"NO"<<endl;
  }
}