#include <iostream>
#include <vector>
bool check(std::vector<std::vector<int>> &a, std::vector<std::vector<int>> &b){
	int n = a.size();
	for(int i = 0; i < n; i++)
		if(a[0][i] != b[0][i])
			for(int j = 0; j < n; j++) a[j][i] ^= 1;	
	for(int i = 1; i < n; i++){
		int need_xor = a[i][0] != b[i][0];
		for(int j = 1; j < n; j++)
			if(need_xor != (a[i][j] != b[i][j])) return false;
	}
	return true;
}
void test_case(){
	int n; std::cin >> n;
	std::vector<std::vector<int>> a(n, std::vector<int>(n)), b(n, std::vector<int>(n));
	for(int i = 0; i < n; i++){
		std::string s; std::cin >> s;
		for(int j = 0; j < n; j++) a[i][j] = s[j] - '0';
	}
	for(int i = 0; i < n; i++){
		std::string s; std::cin >> s;
		for(int j = 0; j < n; j++) b[i][j] = s[j] - '0';
	}
	for(int times = 0; times < 2; times++){
		if(check(a, b)) {std::cout << "YES\n"; return;}
		for(int i = 0; i < n; i++) a[0][i] ^= 1;
	}
	std::cout << "NO\n";
}
int main(){
	std::ios_base::sync_with_stdio(0); std::cin.tie(0);
	int t; std::cin >> t;
	while(t--) test_case();
}