#include <iostream>
#include <cstring>
using namespace std;
#define maxn 10005
int T, n, x, y, z;
int fa[maxn];
void init() {
memset(fa, 0, sizeof(fa));
for(int i = 1; i <= n; i ++)
fa[i] = i;
}
int find(int x) {
if(fa[x] == x) return x;
else return fa[x] = find(fa[x]);
}
void join(int c1, int c2) {
int f1 = find(c1), f2 = find(c2);
if(f1 != f2) fa[f1] = f2;
}
int main() {
cin >> T;
while(T -- ) {
init();
cin >> n;
for(int i = 0; i < n; i ++) {
cin >> x >> y >> z;
if(i != n - 1) {
join(x, y);
} else {
if(z == 1) {
if(find(x) != find(y))
cout << "NO\n";
else
cout << "YES\n";
} else {
if(find(x) == find(y))
cout << "NO\n";
else
cout << "YES\n";
}
}
}
}
return 0;
}