     /* For you,
                who has never forgotten 
                the magic.
                It wants you to know 
                it remembers you too.*/
/*J. M. D.  */
#include <bits/stdc++.h>
using namespace std;
#define int             long long 
#define pll             pair<int,int>
#define ppll            pair<int,pll>
#define vvll            vector<vector<int>>
#define l_b             lower_bound
#define u_b             upper_bound
#define vll             vector<int>
#define vpll            vector<pll>
#define vc              vector <char>
#define vs              vector <string>
#define pb              emplace_back
#define p_b             pop_back()
#define prq             priority_queue
#define setbits(x)      __builtin_popcountll(x)
#define sps(x,y)        fixed<<setprecision(y)<<x
#define hell            1000000007
#define rep(i,s,n)      for(int i=s;i<n;i++)
#define ff              first
#define mem(arr,x)      memset(arr,x,sizeof(arr))
#define ss              second
#define pi              3.14159265358979323846
#define all(v)          v.begin(),v.end()
#define allr(x)         x.rbegin(),x.rend()
#define mp              make_pair
#define display(x)      for(auto dsp:x)cout<<dsp<<" ";
#define maxN            500
#define INF             1000000000000
void fast()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
const int N = 1e3+10;
//---------------------------------------------
int dr[4] = {0, 0, 1,-1};
int dc[4] = {1,-1, 0, 0};
 
int ddr[8]= {0, 0, 1, -1, 1, -1, 1, -1};
int ddc[8]= {1, -1, 0, 0, 1, -1, -1, 1};
 
int ddr1[8]= {1, 1, -1, -1, 2, 2, -2, -2};
int ddc1[8]= {2, -2, 2, -2, 1, -1, 1, -1};
//--------------------------------------------


int32_t main() {
        fast();
        int t,zz;
        cin>>t; 
        rep(zz,1,t+1){
       // cout<<"Case #"<<zz<<": \n";
    int n;
    cin>>n;
    
    string a[n];
    string b[n];
    
    bool a1[n];
    bool b1[n];
    
    b1[0] = 0;
    
    for(int i=0;i<n;i++)
      cin>>a[i];
    
    for(int i=0;i<n;i++)
      cin>>b[i];
      
    for(int j=0;j<n;j++)
    {
      if(a[0][j] != b[0][j])
        a1[j] = 1;
      else
        a1[j] = 0;  
    } 
    
    for(int i=1;i<n;i++)
    {
      if(a[i][0] != b[i][0])
        b1[i] = 1-a1[0];
      else
        b1[i] = a1[0];
    }
    
    bool flag= 0;
    
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<n;j++)
      {
        if((b1[i]+a1[j])%2 == 0 && (a[i][j] != b[i][j]))
          flag = 1;
        
        if((b1[i]+a1[j])%2 == 1 && (a[i][j] == b[i][j]))
          flag = 1;
      } 
    }
    
    if(flag)
      cout<<"NO"<<endl;
    else
      cout<<"YES"<<endl;
  }
  
  return 0;
}