#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=500000+10;
map<int,int>mp;
int a[maxn],b[maxn],p[maxn];
int proa[maxn],prob[maxn];
int tot=0,cnt=0,n,f[maxn];
char ch;
int read(){
int num=0;ch=getchar();
while (ch<'!') ch=getchar();
while (ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}
return num;
}
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
int main(){
freopen("prog.in","r",stdin);
freopen("prog.out","w",stdout);
int t; t=read();
while (t--){
n=read(); tot=0;
mp.clear(); cnt=0;
for (int i=1;i<=n;++i){
a[i]=read(); b[i]=read(); p[i]=read();
if (!mp[a[i]]) mp[a[i]]=++tot;
if (!mp[b[i]]) mp[b[i]]=++tot;
}
for (int i=1;i<=tot;++i) f[i]=i;
for (int i=1;i<=n;++i){
int u=mp[a[i]];
int v=mp[b[i]];
if (p[i]==1){
int p=find(u);
int q=find(v);
if (p!=q) f[p]=q;
}else {
proa[++cnt]=u;
prob[cnt]=v;
}
}
bool flag=1;
for (int i=1;i<=cnt;++i){
int p=find(proa[i]);
int q=find(prob[i]);
if (p==q){
printf("NO\n");
flag=0; break;
}
}
if (flag) printf("YES\n");
}
//system("pause");
}