并查集+map+队列存 e=0
#include<bits/stdc++.h>
#define int long long
#define sd signed
#define db double
#define ld long db
#define il inline
using namespace std;
template<class T=sd>
il T read(){
T f=1,x=0;
char ch=getchar();
while(ch<48||ch>57){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>47&&ch<58) x=(x<<3)+(x<<1)+ch-48,ch=getchar();
return f*x;
}
template<class T>
il void write(T x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9) write((T)(x/10));
putchar(x%10+48);
}
template<class T>il void writech(T x,char ch=' '){write(x),putchar(ch);}
template<class T>il void writeln(T x){write(x),putchar('\n');}
il char gtc(){return getchar();}
il void ptc(char ch){putchar(ch);}
il void ln(){putchar('\n');}
map<int,int> fa;
int find(int x){
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void merge(int x,int y){
sd fx=find(x),fy=find(y);
fa[fy]=fx;
}
struct Node{
int x,y;
Node(int _x=0,int _y=0):x(_x),y(_y){}
};
queue<Node> node;
sd main(){
sd t=read();
while(t--){
fa.clear();
sd n=read();
for(sd i=1;i<=n;i++){
int x=read(),y=read();
bool op=read();
if(op){
if(!fa.count(x)) fa[x]=x;
if(!fa.count(y)) fa[y]=y;
merge(x,y);
}
else node.push(Node(x,y));
}
bool f=1;
while(!node.empty()){
Node i=node.front();
node.pop();
if(find(i.x)==find(i.y)) f=0;
}
if(f) printf("YES\n");
else printf("NO\n");
}
return 0;
}