#include <bits/stdc++.h>
const int N = 4e5 + 5;
using namespace std;
struct E {
  int v, id;
};
vector<int> vc[N];
vector<E> mp[N];
int in[N], n, m;
bool usd[N];
void topsort() {
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (!in[i]) q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : vc[u]) {
      in[v]--;
      if (!in[v]) {
        q.push(v);
      }
    }
    if (mp[u].size()) {
      for (auto e : mp[u]) {
        if (usd[e.id]) continue;
        vc[u].push_back(e.v);
        usd[e.id] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    if (in[i]) {
      puts("NO");
      return;
    }
  puts("YES");
  for (int u = 1; u <= n; u++) {
    for (auto v : vc[u]) {
      printf("%d %d\n", u, v);
    }
  }
}
int xx, yy, zz, T;
int main() {
  cin >> T;
  while (T--) {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      vc[i].clear();
      mp[i].clear();
      in[i] = 0;
    }
    for (int i = 1; i <= m; i++) usd[i] = 0;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &xx, &yy, &zz);
      if (xx)
        vc[yy].push_back(zz), in[zz]++;
      else {
        mp[zz].push_back((E){yy, i});
        mp[yy].push_back((E){zz, i});
      }
    }
    topsort();
  }
  return 0;
}
