#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int N=400010;
int t,n,cnt,num,fa[N],q[N][2];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main()
{
freopen("prog.in","r",stdin);
freopen("prog.out","w",stdout);
scanf("%d",&t);
while (t--){
scanf("%d",&n);
for (int i=1;i<=n*4;i++) fa[i]=i;
map<int,int> M;num=cnt=0;
bool check=1;
for (int k=1;k<=n;k++){
int i,j,e;
scanf("%d%d%d",&i,&j,&e);
if (!check) continue;
if (!M[i]) M[i]=++cnt;i=find(M[i]);
if (!M[j]) M[j]=++cnt;j=find(M[j]);
if (e==1) fa[i]=j;
else num++,q[num][0]=i,q[num][1]=j;
}
for (int i=1;i<=num;i++)
if (find(q[i][0])==find(q[i][1])){
check=0;break;
}
puts(check?"YES":"NO");
}
return 0;
}