思路是先离散化再用并查集
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl '\n'
const ll maxn = 4e05 + 7;
const ll N = 5e05 + 10;
const ll mod = 1e09 + 7;
const int inf = 0x3f3f3f3f;
const ll llinf = 5e18;
typedef pair<int, int>pl;
priority_queue<ll, vector<ll>, greater<ll> >mi; //小根堆
priority_queue<ll> ma;//大根堆
ll gcd(ll a, ll b) {
return b > 0 ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
struct node{
ll x;
ll y;
ll e;
};
bool cmp(node _a,node _b){
return _a.e>_b.e;
}
vector <ll> f(1000005);
ll find(ll x){
if(f[x]==x){
return f[x];
}
else{
f[x]=find(f[x]);
return f[x];
}
}
void solve() {
vector <node> a(1000005);
vector <ll> has(1000005*3);
ll n;
cin >> n;
for(int i=0,j=0;i<n;i++){
cin>>a[i].x>>a[i].y>>a[i].e;
has[j++]=a[i].x;
has[j++]=a[i].y;
}
sort(has.begin(),has.end());
auto last=unique(has.begin(),has.end());
has.erase(last,has.end());
for(int i=0;i<n;i++){
a[i].x=lower_bound(has.begin(),has.end(),a[i].x)-has.begin();
a[i].y=lower_bound(has.begin(),has.end(),a[i].y)-has.begin();
}
sort(a.begin(),a.end(),cmp);
for(int i=0;i<=has.size();i++){
f[i]=i;
}
for(int i=0;i<n;i++){
if(a[i].e==1){
f[find(a[i].x)]=find(a[i].y);
}else{
if(find(a[i].x)==find(a[i].y)){
cout<<"NO"<<endl;
f.clear();
return;
}
}
}
cout<<"YES"<<endl;
f.clear();
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}