#include<bits/stdc++.h>
#define mod 1000000007
#define pi pair<int, int>
#define ll long long int
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define repi(i,a,n) for(int i=a;i<n;i++)
#define repl( i,a, n) for(ll i=a;i<n;i++)
#define pb push_back
#define ff first
#define ss second
using namespace std;
void solve(){
	int n;
	cin>>n;
	
	int a[n][n],b[n][n];
	repi(i,0,n){
	 repi(j,0,n){
	 	char c;
	 	cin>>c;
	  a[i][j]=c-'0';
	}
}

	repi(i,0,n){
	 repi(j,0,n){
	  char c;
	 	cin>>c;
	  b[i][j]=c-'0';
	}
}

	repi(i,0,n){
		if(a[i][0]!=b[i][0]){
			repi(j,0,n){
				a[i][j]=abs(1-a[i][j]);
			}
		}
	}
	repi(i,1,n){
		if(a[0][i]!=b[0][i]){
			repi(j,0,n){

				a[j][i]=abs(1-a[j][i]);
			}
		}
	}

repi(i,0,n)
{ 
	repi(j,0,n) {
	   if(a[i][j]!=b[i][j]){
	   	cout<<"NO"<<endl;
	   	return;
	   }
	
     }
}


cout<<"YES"<<endl;

}
int main(){
	 ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	 #ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif  
	int t;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}