#include<iostream>
#include<vector>
#include<string>
#include<utility>
#include<algorithm>
#define loop(x,i,n) for(int x = i; x < n; ++ x)
#define looprev(x,i,n) for(int x = i;x>=n;x--)
using namespace std;
typedef long long ll;
vector <int> memo;
vector <bool> out;
int dp(int);
int main()
{
    ll size;
    cin>>size;
    while(size-->0)
    {
        int n;
        cin>>n;
        //cout<<"n is "<<n<<"\n";
        if(n==1)
        {
            cin>>n>>n;
            out.push_back(true);
            continue;
        }
        string temp ="";
        vector<vector<int> >M (n);
        loop(i,0,n)
        {
            cin>>temp;
            //cout<<"temp is "<<temp<<"\n";
            loop(j,0,n)
                M[i].push_back((temp.at(j)=='0')?0:1);
        }

        loop(i,0,n)
        {
            cin>>temp;
            //cout<<"temp is "<<temp<<"\n";
            loop(j,0,n)
            {
                int x = (temp.at(j)=='0')?0:1;
                M[i][j] = M[i][j] ^ x;
            }
        }

        vector<int> XOR = M[0];
        bool flag = true;
        loop(i,1,n)
        {
            loop(j,1,n)
            {
                if(M[i][j] ^ M[i][j-1] != XOR[j] ^ XOR[j-1])
                {
                    flag = false;
                    break;
                }
            }
            if(!flag)
                break;
        }
        out.push_back(flag);
    }
    for(bool i: out)
    {
        cout<<(i?"YES":"NO")<<"\n";
    }

    return 0;
}