#include <bits/stdc++.h>
using namespace std;
int n, m, u[500000], v[500000], w[500000], in_deg[500000], un[500000];
vector<int> adj[500000];
int cnt;
int top_sort() {
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (in_deg[i] == 0) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    int a = q.front();
    cnt++;
    un[a] = cnt;
    q.pop();
    for (int i = 0; i < adj[a].size(); i++) {
      in_deg[adj[a][i]]--;
      if (in_deg[adj[a][i]] <= 0) {
        q.push(adj[a][i]);
      }
    }
  }
  return cnt == n;
}
void solve() {
  cnt = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    in_deg[i] = 0;
    adj[i].clear();
  }
  for (int i = 0; i < m; i++) {
    cin >> w[i] >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    if (w[i]) {
      adj[u[i]].push_back(v[i]);
      in_deg[v[i]]++;
    }
  }
  if (!top_sort()) {
    cout << "NO" << '\n';
    return;
  }
  cout << "YES" << '\n';
  for (int i = 0; i < m; i++) {
    if (un[u[i]] <= un[v[i]]) {
      cout << u[i] + 1 << ' ' << v[i] + 1 << '\n';
    } else {
      cout << v[i] + 1 << ' ' << u[i] + 1 << '\n';
    }
  }
}
int main() {
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
