#include <map>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 10;
int f[N];
map<int, int> mp;
template <typename T>
inline T
read()
{
T sum = 0, sig = 1;
int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') sig = -1;
for (; isdigit(ch); ch = getchar())
sum = sum * 10 + ch - '0';
return sum * sig;
}
int find(int x)
{
return (f[x] == x) ? f[x] : f[x] = find(f[x]);
}
signed main()
{
int t = read<int>();
while (t--)
{
mp.clear();
int flag = 0, cnt = 0;
int n = read<int>();
for (int i = 1; i <= (2 * n); i++)
f[i] = i;
for (int i = 1; i <= n; i++)
{
int x = read<int>();
int y = read<int>();
int e = read<int>();
if (mp.find(x) != mp.end())
{
x = mp[x];
}
else
mp[x] = ++cnt, x = cnt;
if (mp.find(y) != mp.end())
{
y = mp[y];
}
else
mp[y] = ++cnt, y = cnt;
x = find(x), y = find(y);
if (!e)
{
if (x == y)
flag = 1;
}
else
f[x] = y;
}
if (flag)
puts("NO");
else
puts("YES");
}
return 0;
}#include <map>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 10;
int f[N];
map<int, int> mp;
template <typename T>
inline T
read()
{
T sum = 0, sig = 1;
int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') sig = -1;
for (; isdigit(ch); ch = getchar())
sum = sum * 10 + ch - '0';
return sum * sig;
}
int find(int x)
{
return (f[x] == x) ? f[x] : f[x] = find(f[x]);
}
signed main()
{
int t = read<int>();
while (t--)
{
mp.clear();
int flag = 0, cnt = 0;
int n = read<int>();
for (int i = 1; i <= (2 * n); i++)
f[i] = i;
for (int i = 1; i <= n; i++)
{
int x = read<int>();
int y = read<int>();
int e = read<int>();
if (mp.find(x) != mp.end())
{
x = mp[x];
}
else
mp[x] = ++cnt, x = cnt;
if (mp.find(y) != mp.end())
{
y = mp[y];
}
else
mp[y] = ++cnt, y = cnt;
x = find(x), y = find(y);
if (!e)
{
if (x == y)
flag = 1;
}
else
f[x] = y;
}
if (flag)
puts("NO");
else
puts("YES");
}
return 0;
}