#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <climits>
#include <iomanip>
#define endl "\n"
#define debug(x) cout << #x << " : " << x << endl;
#define debug2(x, y) cout << #x << " : " << x  << ", " << #y << " : " << y << endl;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(0);
typedef long long ll;
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	FASTIO

	int t;
	cin >> t;

	while(t --)
	{
		int n;
		cin >> n;

		char a[n + 1][n + 1];
		char b[n + 1][n + 1];
		for(int i = 0; i < n; i ++)
		{
			for(int j = 0; j < n; j ++)
			{
				cin >> a[i][j];
			}
		}

		for(int i = 0; i < n; i ++)
		{
			for(int j = 0; j < n; j ++)
			{
				cin >> b[i][j];
			}
		}

		// Vertical Swaps
		for(int i = 0; i < n; i ++)
		{
			if(a[0][i] != b[0][i])
			{
				for(int j = 0; j < n; j ++)
				{
					if(a[j][i] == '1')
						a[j][i] = '0';
					else
						a[j][i] = '1';
				}
			}
		}

		// Horizontal Swaps
		for(int i = 0; i < n; i ++)
		{
			if(a[i][0] != b[i][0])
			{
				for(int j = 0; j < n; j ++)
				{
					if(a[i][j] == '1')
						a[i][j] = '0';
					else
						a[i][j] = '1';
				}
			}
		}

		int flag = 0;
		for(int i = 0; i < n; i ++)
		{
			for(int j = 0; j < n; j ++)
			{
				if(a[i][j] != b[i][j])
				{
					flag = 1;
					break;
				}
			}

			if(flag == 1)
				break;
		}

		if(flag == 1)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}	
}