#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[200005];
int indeg[200005];
int pos[200005];
bool topsort() {
  set<pair<int, int> > second;
  for (int i = 1; i <= n; i++) {
    second.insert({indeg[i], i});
  }
  int cnt = 0;
  while (!second.empty()) {
    int x = second.begin()->first;
    int u = second.begin()->second;
    if (x != 0) return 0;
    pos[u] = ++cnt;
    second.erase({x, u});
    for (int v : g[u]) {
      second.erase({indeg[v], v});
      indeg[v]--;
      second.insert({indeg[v], v});
    }
  }
  return 1;
}
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  };
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      g[i].clear();
      indeg[i] = 0;
    }
    tuple<int, int, int> tp[m + 5];
    for (int i = 0; i < m; i++) {
      int t;
      cin >> t;
      int u, v;
      cin >> u >> v;
      tp[i] = make_tuple(t, u, v);
      if (t) {
        g[u].push_back(v);
        indeg[v]++;
      }
    }
    if (!topsort())
      cout << "NO" << endl;
    else {
      cout << "YES" << endl;
      for (int i = 0; i < m; i++) {
        if (get<0>(tp[i])) {
          cout << get<1>(tp[i]) << ' ' << get<2>(tp[i]) << endl;
        } else {
          int u = get<1>(tp[i]);
          int v = get<2>(tp[i]);
          if (pos[u] < pos[v]) {
            cout << u << ' ' << v << endl;
          } else {
            cout << v << ' ' << u << endl;
          }
        }
      }
    }
  }
}
