#include<bits/stdc++.h>

#define ld long double
#define ll long long
using namespace std;
template<class T>
void read(T& x)
{
	T res = 0, f = 1; char c = getchar();
	while (!isdigit(c)) {
		if (c == '-')f = -1; c = getchar();
	}
	while (isdigit(c)) {
		res = (res << 3) + (res << 1) + c - '0'; c = getchar();
	}
	x = res * f;
}
const ll N = 2000 + 10;
const int mod = 1e9 + 7;
int f[N * 10];
int t, n, a[N][N], b[N][N];
int find(int x)
{
	return x == f[x] ? f[x] : f[x] = find(f[x]);
}
#define r(x) (x+n)
#define c(x) (x)
#define fm(x) (x+2*n)
int main()
{
	//ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
	freopen("test.in", "r", stdin);
#endif // ONLINE_JUDGE
	read(t);
	while (t--)
	{
		read(n);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				scanf("%1d", &a[i][j]);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				scanf("%1d", &b[i][j]);
		for (int i = 1; i <= n; i++)
		{
			f[r(i)] = r(i);
			f[fm(r(i))] = fm(r(i));
			f[c(i)] = c(i);
			f[fm(c(i))] = fm(c(i));
		}
		bool isok = 1;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (a[i][j] == b[i][j])
				{
					if (find(r(i)) == find(fm(c(j))) || find(c(j)) == find(fm(r(i))))isok = 0;
					f[find(r(i))] = find(c(j));
					f[find(fm(r(i)))] = find(fm(c(j)));
				}
				else
				{
					if (find(r(i)) == find((c(j))) || find(fm(c(j))) == find(fm(r(i))))isok = 0;
					f[find(r(i))] = find(fm(c(j)));
					f[find(fm(r(i)))] = find(c(j));
				}
			}
		}
		printf("%s\n", isok ? "YES" : "NO");
	}

	return 0;
}
