#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s)    (int)(s.size())
//#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define mk(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("gcd.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}

void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
string flib(const string& str) {
	string ret = "";
	for (auto& ch : str)
		ret.push_back(1 - (ch - '0') + '0');
	return ret;
}
vector<string> a, b;
int n;
bool test_case() {
	cin >> n;
	a = b = vector<string>(n);
	for (auto& it : a) cin >> it;
	for (auto& it : b) cin >> it;
	/*bool ok = true;
	for (int i = 0; i < n; i++)
		ok &= (a[i] == b[i] || flib(a[i]) == b[i]);
	if (ok) return true;*/
	vi row(n), col(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (a[i][j] == b[i][j]) {
				row[i] = col[j] = true;
				continue;
			}
			if (row[i] && col[j]) return false;
			if (!row[i])
				a[i] = flib(a[i]);
			else {
				for (int k = 0; k < n; k++)
					a[k][j] = (1 - (a[k][j] - '0') + '0');
			}
			row[i] = col[j] = 1;
		}
	}
	return true;
}
int main() {
	file();
	fast();
	int t; cin >> t;
	while (t--) {
		cout << (test_case() ? "YES\n" : "NO\n");
	}
	return 0;
}