#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int, int>father;
struct node
{
int e,x,y;
};
vector<node>aaa;
int find(int x) {
return x == father[x] ? x : father[x] = find(father[x]);
}
void unionn(int x, int y) {
father[find(x)] = find(y);
}
void work(void)
{
aaa.clear();
father.clear();
int tot=0;
int n;
cin>>n;
for(int i=1;i<=2*n;i++)
{
father[i]=i;
}
for(int i=1;i<=n;i++)
{
int e,x,y;
cin>>x>>y>>e;
if(e==1)
{
unionn(x,y);
}
else if(e==0)
{
tot++;
aaa.push_back(node{e,x,y});
}
}
for(int i=0;i<tot;i++)
{
if(aaa[i].e==0&&find(aaa[i].x)==find(aaa[i].y))
{
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
work();
}
}