#include<bits/stdc++.h>
#define int long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define mod 1000000007
#define rep(i,a,b) for(auto i=a;i<b;i++)
using namespace std;
using pii = pair<int,int>;
// to do lucas theorem and ncr with modulus

string solve()
{
    int n;
    cin>>n;
    vector<vector<int> > A(n,vector<int>(n,0)),B(n,vector<int>(n,0));
    string ss;
    rep(i,0,n){
        cin>>ss;
        rep(j,0,n) A[i][j]=ss[j]-'0';
    }
    rep(i,0,n){
        cin>>ss;
        rep(j,0,n) B[i][j]=ss[j]-'0';
    }

    rep(j,0,n)
        if(A[0][j]!=B[0][j])
            rep(i,0,n) A[i][j]^=1;

    rep(i,1,n)
    {
        int c=0;
        rep(j,0,n) if(A[i][j]!=B[i][j]) c++;

        if(c==0 || c==n) continue;
        else return "no";
    }    
    return "yes";
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
    
 
	int tt;
	cin>>tt;
	while(tt--){
        cout<<solve()<<endl;
	}
	return 0;
}