# include <bits/stdc++.h>
using namespace std;

#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define eb          emplace_back
#define endl		'\n'
#define max(a,b)	((a)>(b)?(a):(b))
#define min(a,b)	((a)<(b)?(a):(b))
#define vi          vector<int>
#define vii         vector<pair<int, int> >
#define mp          make_pair
#define mem1(a)     memset((a),-1,sizeof(a))
#define mem0(a)     memset((a),0,sizeof(a))
#define memf(a)     memset((a),false,sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    (a)*((b)/gcd((a),(b)))
#define DECIMAL(n)  cout << fixed ; cout << setprecision(n);
#define FAST        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mi          map<int,int>
#define sz(x)       (ll)(x).size()
#define rep(i,a,b)  for(int i=(a);i<(b);i++)
#define pii         pair<int,int>

int solve()
{
	int n;
	cin >> n;
	string s[n];
	string a[n];
	rep(i, 0, n)
	cin >> s[i];
	rep(i, 0, n)
	cin >> a[i];
	rep(i, 0, n)
	{
		if (s[0][i] != a[0][i])
		{
			rep(j, 0, n)
			{
				s[j][i] = '1'^'0'^s[j][i];
			}
		}
	}
	rep(i, 1, n)
	{
		int r = 0, e = 0;
		rep(j, 0, n)
		{
			if (s[i][j] != a[i][j])
				r++;
			else
				e++;
		}
		if (!(e == n || r == n))
		{
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}
signed main()
{
	FAST
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int t = 1;
	cin >> t;
	while (t)
	{
		solve();
		t--;
	}
	return 0;
}