#include <iostream>
#include <list>
#include <map>
#include <ranges>
#include <vector>
#define sv std::ranges::views::
#define cid(x)             \
  if (id[x]) {             \
    x = id[x];             \
  } else {                 \
    x = id[x] = id.size(); \
  }

using namespace std;
using tp = long long int;

vector<tp> f;

void init(tp n) {
  f = vector<tp>(n + 1);
  for (tp i = 0; i <= n; ++i) {
    f[i] = i;
  }
}

tp find(tp x) {
  return f[x] == x ? x : f[x] = find(f[x]);
}

void merge(tp u, tp v) {
  f[find(v)] = find(u);
}

void Core() {
  tp n;
  list<pair<tp, tp>> c;
  map<tp, tp> id;
  scanf("%lld", &n);
  init(n + 10000000);
  while (n--) {
    tp x, y, t;
    scanf("%lld%lld%lld", &x, &y, &t);
    cid(x);
    cid(y);
    if (t) {
      merge(x, y);
    } else {
      c.emplace_back(x, y);
    }
  }
  for (auto&& [x, y] : c) {
    if (find(x) == find(y)) {
      puts("NO");
      return;
    }
  }
  puts("YES");
}

signed main() {
  tp t;
  scanf("%lld", &t);
  while (t--) {
    Core();
  }
  return EXIT_SUCCESS;
}

struct __Initer_t__ {
  __Initer_t__() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
  }
} __Initer__;

/*
 */

/*#################################################################
#.................................................................#
#............................This.Code.Was.Created.By.RBTree......#
#.............#......#...............Limiting-Factor..............#
#............#.#....#.#.................Soul-Code.................#
#.............########............................................#
#............#........#..##############################...........#
#...........#..V....V......#..#........................#..#...#...#
#............#........#....#..........###..###..........#..#.#.#..#
#............#..X##X..#..#............#....#.#...........#..#...#.#
#...........#...N##N...#..#...........###..###..........#.........#
#.......MOE..#..@.....#....#.#.#.#...................#.#..........#
#.............########.....#.#.#.##############.#.#..#.#..........#
#..........................#.#.#.#.............#.#.#.#.#..........#
#......#########...........#.#.#.#.................#.#.#..........#
#.....#.........#..........#.#.#.#.................#.#.#..........#
#.#.#.#G#R#A#S#S#.#.#......#.#.#.#.................#.#.#..........#
#.###################......#.#.#.#.................#.#.#..........#
#...........................#.#.#...................#.#...........#
#.................................................................#
#################################################################*/