#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 100002
#define mod 1000000007
#define INF 1000000007
#define pb push_back
#define ff first
#define ss second
#define w(tt) int tt;cin>>tt;while(tt--)
#define endl  "\n"
#define IOS  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void solve(){
	 int n;
	 cin>>n;
	 char x;
	 vector<vector<int>> to(n,vector<int>(n)),frm(n,vector<int>(n));
	 for(int i=0;i<n;i++){
		 for(int j=0;j<n;j++){cin>>x;frm[i][j]=(x-'0');}
		 }
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){cin>>x;to[i][j]=(x-'0');
			}
		}
	for(int i=0;i<n;i++){
		if(frm[0][i]!=to[0][i]){
			for(int j=0;j<n;j++)frm[j][i]^=1;
			}
		}
	int f=0;
	for(int i=1;i<n;i++){
		vector<int> v1,v0,v;
		for(int j=0;j<n;j++){
			v1.pb(frm[i][j]^1);
			v0.pb(frm[i][j]);
		}
		for(int j=0;j<n;j++)v.pb(to[i][j]);
		if(v0!=v && v1!=v)f=1;
		}
		if(f==0)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
int32_t main(){
	IOS;
	w(tt){solve();}
	//solve();
	return 0;
	}








