#include <bits/stdc++.h>
using namespace std;
int n;
int m;
struct Edge {
  int x;
  int y;
  int t;
  Edge(int x, int y, int t) : x(x), y(y), t(t) {}
  void print() { cout << x << " " << y << " " << t << endl; }
};
vector<Edge> edges;
vector<int> edge_idx[200005];
int in_digree[200005];
queue<int> Q;
int order[200005];
int main() {
  int i, j, k;
  int x, y, z;
  int t;
  cin >> t;
  while (0 < t--) {
    edges.clear();
    while (!Q.empty()) Q.pop();
    for (i = 0; i < n; i++) edge_idx[i].clear();
    memset(in_digree, 0, sizeof(in_digree));
    cin >> n >> m;
    for (i = 0; i < m; i++) {
      cin >> z >> x >> y;
      x--;
      y--;
      Edge e(x, y, z);
      if (e.x != x || e.y != y || e.t != z) return -1;
      edge_idx[x].push_back(edges.size());
      edge_idx[y].push_back(edges.size());
      edges.push_back(e);
      if (z) {
        in_digree[y] += 1;
      }
    }
    for (i = 0; i < n; i++) {
      if (in_digree[i] == 0) {
        Q.push(i);
      }
    }
    int cnt = 0;
    k = 0;
    while (!Q.empty()) {
      x = Q.front();
      Q.pop();
      order[x] = k++;
      cnt++;
      for (i = 0; i < edge_idx[x].size(); i++) {
        Edge e = edges[edge_idx[x][i]];
        z = -1;
        if (e.t == 1 && e.x == x) {
          y = e.y;
          if (--in_digree[y] == 0) Q.push(y);
        }
      }
    }
    if (cnt != n) {
      cout << "NO\n";
    } else {
      cout << "YES\n";
      for (i = 0; i < edges.size(); i++) {
        Edge e = edges[i];
        if (order[e.x] < order[e.y]) {
          cout << e.x + 1 << " " << e.y + 1 << endl;
        } else {
          cout << e.y + 1 << " " << e.x + 1 << endl;
        }
      }
    }
  }
  return 0;
}
