#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
long dc[maxn*2], top = 0;
int n;
long di[maxn], dj[maxn], de[maxn];
inline long read()
{
long a = 0; int c;
do c = getchar(); while (!isdigit(c));
while (isdigit(c)) {
a = a*10 + c - '0';
c = getchar();
}
return a;
}
int dc_val(long i)
{ return lower_bound(dc+1, dc+top+1, i)-dc; }
int fa[maxn*2];
int findf(int i)
{ return fa[i]?fa[i]=findf(fa[i]):i; }
int main()
{
freopen("prog.in", "r", stdin);
freopen("prog.out", "w", stdout);
int T;
cin >> T;
//cout << T << endl;
while (T--) {
cin >> n;
memset(fa, 0, sizeof fa);
top = 0;
for (register int i = 1; i <= n; i++) {
di[i] = read(); dj[i] = read(); de[i] = read();
dc[++top] = di[i], dc[++top] = dj[i];
}
sort(dc+1, dc+top+1);
top = unique(dc+1, dc+top+1) - dc - 1;
for (register int i = 1; i <= n; i++) {
if (!de[i]) continue;
int ni = dc_val(di[i]), nj = dc_val(dj[i]);
if (findf(ni) != findf(nj))
fa[findf(ni)] = findf(nj);
}
bool right = 1;
for (register int i = 1; i <= n; i++) {
if (de[i]) continue;
int ni = dc_val(di[i]), nj = dc_val(dj[i]);
if (findf(ni) == findf(nj)) {
right = 0;
break;
}
}
if (right) puts("YES");
else puts("NO");
}
return 0;
}