/ Problem: 1059 User: saruka Memory: 256K Time: 5MS Language: G++ Result: Accepted /
include
int getint()
{
int r = 0, k = 1;
char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if(c == '-') k = -1;
for(; c >= '0' && c <= '9'; c = getchar())
r = r 10 + c - '0';
return r k;
}
const int maxn = 1000100;
const int mod = 1000000;
int n, T, x, y, e, cnt;
int ueq[maxn][2], fa[2 maxn];
inline int hash(int x)
{
return x > mod ? (mod + x / mod + x % mod): x;
}
int getfather(int x)
{
return fa[x] = (fa[x] == x) ? x : getfather(fa[x]);
}
int main()
{
T = getint();
while(T--)
{
cnt = 0;
n = getint();
for(int i = 1; i < 2 maxn; i++) fa[i] = i;
for(int i = 1; i <= n; i++)
{
x = getint();
y = getint();
e = getint();
x = hash(x);
y = hash(y);
if(e == 1)
{
fa[getfather(x)] = getfather(y);
}
else
{
++cnt;
ueq[cnt][0] = x;
ueq[cnt][1] = y;
}
}
bool flag = 1;
for(int i = 1; i <= cnt; i++)
{
if(getfather(ueq[i][0]) == getfather(ueq[i][1]))
{
flag = 0;
break;
}
}
printf(flag ? ("YES\n") : ("NO\n"));
}
return 0;
}