#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<int> adj[n + 1];
    vector<int> indegree(n + 1, 0);
    vector<pair<int, int>> edges;
    for (int i = 0; i < m; i++) {
      int q, x, y;
      cin >> q >> x >> y;
      edges.push_back({x, y});
      if (q == 1) {
        adj[x].push_back(y);
        indegree[y]++;
      }
    }
    vector<int> topo;
    queue<int> q;
    for (int i = 1; i <= n; i++) {
      if (indegree[i] == 0) {
        q.push(i);
      }
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      topo.push_back(v);
      for (auto u : adj[v]) {
        indegree[u]--;
        if (indegree[u] == 0) {
          q.push(u);
        }
      }
    }
    if (topo.size() < n) {
      cout << "NO" << endl;
    } else {
      cout << "YES" << endl;
      map<int, int> ind;
      for (int i = 0; i < n; i++) {
        ind[topo[i]] = i;
      }
      for (int i = 0; i < edges.size(); i++) {
        if (ind[edges[i].first] > ind[edges[i].second]) {
          cout << edges[i].second << " " << edges[i].first << endl;
        } else {
          cout << edges[i].first << " " << edges[i].second << endl;
        }
      }
    }
  }
  return 0;
}
