#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m, t, x, y;
  ;
  cin >> n >> m;
  vector<vector<int>> graph(n + 1);
  vector<pair<int, int>> edg;
  vector<int> indeg(n + 1, 0);
  for (int i = 0; i < m; i++) {
    cin >> t >> x >> y;
    if (t == 1) {
      graph[x].push_back(y);
      indeg[y]++;
    } else {
      edg.push_back({x, y});
    }
  }
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indeg[i] == 0) q.push(i);
  }
  vector<int> order(n + 1, 0);
  int sz = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    order[u] = sz++;
    for (auto v : graph[u]) {
      if (--indeg[v] == 0) {
        q.push(v);
      }
    }
  }
  if (sz != n) {
    cout << "NO\n";
  } else {
    cout << "YES\n";
    for (auto pp : edg) {
      int u = pp.first;
      int v = pp.second;
      if (order[u] < order[v]) {
        graph[u].push_back(v);
      } else {
        graph[v].push_back(u);
      }
    }
    for (int i = 1; i <= n; i++) {
      for (auto v : graph[i]) {
        cout << i << " " << v << "\n";
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
