#include <iostream>
#include <unordered_map>
using namespace std;
unordered_map<int, int> itoi;
struct node {
int a, b;
bool e;
} eq[2000001];
int eqtop;
#include <vector>
class unionfind {
private:
int _setcount;
vector <int> father, size;
int findfather(int n){
if(this -> father[n] == n)
return n;
else
return this -> father[n] = findfather(this -> father[n]);
}
public:
void init(int size, int begin = 0){
this -> _setcount = size;
this -> father.clear();
this -> size.clear();
for(int i = 0; i < size + begin; i ++){
this -> father.push_back(i);
this -> size.push_back(1);
}
}
bool connected(int n, int m){
return findfather(n) == findfather(m);
}
bool connected(){
return this -> _setcount == 1;
}
void merge(int n, int m){
if(!connected(n, m))
-- _setcount;
int nroot = findfather(n);
int mroot = findfather(m);
if(nroot != mroot){
if(this -> size[nroot] > this -> size[mroot])
swap(nroot, mroot);
this -> father[nroot] = mroot;
this -> size[mroot] += this -> size[nroot];
}
}
int setcount(){
return this -> _setcount;
}
} uf;
int main(){
int T;
cin >> T;
while(T--){
itoi.clear();
eqtop = 0;
int n;
cin >> n;
for(int i = 0; i < n; ++ i){
int a, b;
bool e;
cin >> a >> b >> e;
eq[eqtop].a = a;
eq[eqtop].b = b;
eq[eqtop ++].e = e;
if(itoi.find(a) != itoi.end()) itoi[a] = itoi.size();
if(itoi.find(b) != itoi.end()) itoi[b] = itoi.size();
}
uf.init(2 * n, 0);
for(int i = 0; i < n; ++ i)
if(eq[i].e)
uf.merge(itoi[eq[i].a], itoi[eq[i].b]);
bool flag = false;
for(int i = 0; i < n; ++ i)
if(!eq[i].e)
if(uf.connected(itoi[eq[i].a], itoi[eq[i].b])){
cout << "NO" << endl;
flag = true;
break;
}
if(flag)
continue;
cout << "YES" << endl;
}
}