#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e7 + 10;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-'){ w = -1;} ch = getchar();}
while(ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return s * w;
}
int n, fa[N];
int low[N], a[N], e[N];
int tl[N][3];
int find(int u)
{
return (u == fa[u] ? u : fa[u] = find(fa[u]));
}
bool fl = 1;
int nn;
void merge(int x, int y)
{
int fx = find(x), fy = find(y);
if(fx == fy) return ;
if(abs(fx - fy) == nn) fl = 0;
fa[fx] = fy;
}
void solve()
{
fl = 1;
cin >> n;
nn = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[++nn];
tl[i][0] = a[nn];
cin >> a[++nn] >> e[i];
tl[i][1] = a[nn];
}
for(int i = 1; i <= (nn << 1); i++) fa[i] = i;
sort(a + 1, a + nn + 1);
for(int i = 1; i <= nn; i++) low[a[i]] = i;
for(int i = 1; i <= n; i++)
{
int x = low[tl[i][0]], y = low[tl[i][1]];
if(e[i])
{
merge(x, y);
merge(x + nn, y + nn);
}
else
{
merge(x, y + nn);
merge(x + nn, y);
}
if(!fl) return puts("NO"), void();
}
puts("YES");
}
signed main()
{
int T = read();
while(T--) solve();
return 0;
}