#include <bits/stdc++.h>
using namespace std;
vector<int> e[200005];
int u[200005], v[200005];
int T, n, m;
int in[200005], tp[200005];
queue<int> q;
bool topsort() {
  int ct = 0, tmp = 0;
  for (int i = 1; i <= n; i++)
    if (!in[i]) q.push(i), ct++, tp[i] = ++tmp;
  while (!q.empty()) {
    int U = q.front();
    q.pop();
    for (int V : e[U]) {
      in[V]--;
      if (!in[V]) q.push(V), ct++, tp[V] = ++tmp;
    }
  }
  return ct < n;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    memset(in, 0, sizeof(in));
    for (int i = 1; i <= n; i++) e[i].clear();
    for (int o, i = 1; i <= m; i++) {
      scanf("%d%d%d", &o, &u[i], &v[i]);
      if (o) in[v[i]]++, e[u[i]].emplace_back(v[i]);
    }
    if (topsort())
      puts("NO");
    else {
      puts("YES");
      for (int i = 1; i <= m; i++)
        if (tp[u[i]] < tp[v[i]])
          printf("%d %d\n", u[i], v[i]);
        else
          printf("%d %d\n", v[i], u[i]);
    }
  }
  return 0;
}
