#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> deg(n), idx(n);
  vector<int> e[n];
  vector<pair<int, int> > ask;
  for (int i = 0; i < m; i++) {
    int cmd, u, v;
    cin >> cmd >> u >> v;
    u--, v--;
    if (cmd == 0) {
      ask.push_back({u, v});
    } else {
      ask.push_back({u, v});
      e[u].push_back(v);
      deg[v]++;
    }
  }
  queue<int> q;
  int tot = 0;
  for (int i = 0; i < n; i++)
    if (deg[i] == 0) q.push(i);
  while (q.empty() == false) {
    int u = q.front();
    q.pop();
    idx[u] = tot++;
    for (int v : e[u]) {
      deg[v]--;
      if (deg[v] == 0) q.push(v);
    }
  }
  if (tot != n) {
    cout << "NO\n";
    return;
  }
  cout << "YES\n";
  for (auto &[u, v] : ask) {
    if (idx[u] < idx[v])
      cout << u + 1 << ' ' << v + 1 << endl;
    else
      cout << v + 1 << ' ' << u + 1 << endl;
  }
}
signed main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
