#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N],c[N]; int main() { int t,n,x; scanf("%d",&t); while(t--) { bool f=0; scanf("%d",&n); for(int i=1;i<=n;i++) { a[i]=0; for(int j=1;j<=n;j++) { scanf("%d",&x); a[i]^=x; } } for(int j=1;j<=n;j++)scanf("%d",&x),c[j]=a[1]^x; for(int i=2;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&x); if(c[j]!=(a[i]^x))f=1; } } if(f)puts("NO"); else puts("YES"); } return 0; }