/*  
             ____     __                       __
     /\     |  __>   /    |   |    /\    |    /
    /  \    |  \    <     |---|   /  \   |   <
   /    \   |   \    \__  |   |  /    \  |    \__

         _   __             ____    ___
        | \_/ |    /\     /        |
        |     |   /  \   <  <-->   |---
        |     |  /    \   \__\ |   |___

  Author::::::::: Archaic_Mage :::::::::Author

     _____    __________  _________     ___ ___      _____    .___  _________  
   /  _  \   \______   \ \_   ___ \   /   |   \    /  _  \   |   | \_   ___ \ 
  /  /_\  \   |       _/ /    \  \/  /    ~    \  /  /_\  \  |   | /    \  \/ 
 /    |    \  |    |   \ \     \____ \    Y    / /    |    \ |   | \     \____
 \____|__  /  |____|_  /  \______  /  \___|_  /  \____|__  / |___|  \______  /
         \/          \/          \/         \/           \/                \/ 
               _____       _____      ________  ___________                  
              /     \     /  _  \    /  _____/  \_   _____/                  
             /  \ /  \   /  /_\  \  /   \  ___   |    __)_                   
            /    Y    \ /    |    \ \    \_\  \  |        \                  
            \____|__  / \____|__  /  \______  / /_______  /                  
                    \/          \/          \/          \/                   


*/

#include <bits/stdc++.h>

typedef long long ll;
typedef int64_t i64;
typedef unsigned long long ull;

#define f(i, a, b) for (ll i= a; i < b; i++)
#define fd(i, a, b) for(ll i = a; i >= b; i--)
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pss pair<string, string>
#define pcc pair<char, char>
#define p pair
#define vpii vector<pii>
#define vi vector<int>
#define vc vector<char>
#define vll vector<ll>
#define v vector

using namespace std;

bool check(vector<vector<int>> a, vector<vector<int>> const &b) {
  int n = (int) a.size();
  for (int j = 0; j < n; j++) {
    if (a[0][j] != b[0][j]) {
      for (int i = 0; i < n; i++) {
        a[i][j] ^= 1;
      }
    }
  }
  for (int i = 0; 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 solve() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  vector<vector<int>> b(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      a[i][j] = s[j] - '0';
    }
  }
  for (int i = 0; i < n; i++) {
    string s;
    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)) {
      cout << "YES\n";
      return;
    }
    for (int j = 0; j < n; j++) {
      a[0][j] ^= 1;
    }
  }
  cout << "NO\n";
}

int main() {
  int test;
  cin >> test;
  while (test-- > 0) {
    solve();
  }
  return 0;
}