1 #include <cstdio>
2 #include <algorithm>
3 #include <map>
4 using namespace std;
5 int a[1000005][2], fa[2000005];
6 bool op[1000005];
7 inline int getfa(int x) {
8 return x == fa[x] ? x : fa[x] = getfa(fa[x]);
9 }
10
11 int main() {
12 int T, n, cd = 0;
13 scanf("%d", &T);
14 while(T--) {
15 bool flag = true;
16 map<int, int> Map;
17 scanf("%d", &n);
18 for(int i = 1; i <= n; i++) {
19 int tmp;
20 scanf("%d%d%d", &a[i][0], &a[i][1], &tmp);
21 op[i] = tmp;
22 if(!Map[a[i][0]]) Map[a[i][0]] = ++cd;
23 if(!Map[a[i][1]]) Map[a[i][1]] = ++cd;
24 }
25 for(int i = 1; i <= cd; i++)
26 fa[i] = i;
27 for(int i = 1; i <= n; i++)
28 if(op[i]) {
29 int p1 = getfa(Map[a[i][0]]), p2 = getfa(Map[a[i][1]]);
30 fa[p1] = p2;
31 }
32 for(int i = 1; i <= n; i++)
33 if(!op[i]) {
34 int p1 = getfa(Map[a[i][0]]), p2 = getfa(Map[a[i][1]]);
35 if(p1 == p2) {
36 flag = false; break;
37 }
38 }
39 cd = 0;
40 if(flag) puts("YES");
41 else puts("NO");
42 }
43 return 0;
44 }