#include<bits/stdc++.h>
using namespace std;
int t;
int n;
struct sb{
int x,y,e;
}a[100086];
bool cnm(sb x,sb y){
return x.e<y.e;
}
int f[100086];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
void zyc(){
map<int,int>b;
int tot=0;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y>>a[i].e;
for(int i=1;i<=n;i++){
if(a[i].e==1){
int x=a[i].x,y=a[i].y;
if(b[x]==0)b[x]=++tot;
if(b[y]==0)b[y]=++tot;
int s1=find(b[x]);
int s2=find(b[y]);
f[s2]=b[s1];
}
else{
int x=find(b[a[i].x]),y=find(b[a[i].y]);
if(x==y){
puts("NO");
return;
}
}
}puts("YES");
}
int main(){
cin>>t;
while(t--){
zyc();
}
}