#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
vector<int> badj[MAXN];
vector<int> adj[MAXN];
bool mark[MAXN];
int a[MAXN];
int t, n, m;
int chert;
vector<int> vc;
vector<pair<int, int>> e;
void input();
void dfs(int);
void bdfs(int);
void solve();
void reset();
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  input();
  return 0;
}
void input() {
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      int x, u, v;
      cin >> x >> u >> v;
      u--, v--;
      if (x) adj[u].push_back(v), badj[v].push_back(u);
      e.push_back({u, v});
    }
    solve();
    reset();
  }
}
void dfs(int u) {
  mark[u] = true;
  for (auto v : adj[u])
    if (!mark[v]) dfs(v);
  a[u] = ++chert;
  vc.push_back(u);
}
void bdfs(int u) {
  mark[u] = true;
  for (auto v : badj[u])
    if (!mark[v]) bdfs(v);
}
void solve() {
  for (int i = 0; i < n; i++)
    if (!mark[i]) dfs(i);
  for (int i = 0; i < n; i++) mark[i] = false;
  int cnt = 0;
  for (int i = vc.size() - 1; i >= 0; i--)
    if (!mark[vc[i]]) bdfs(vc[i]), cnt++;
  if (cnt != n) {
    cout << "NO" << endl;
    return;
  }
  cout << "YES" << endl;
  for (auto ed : e) {
    int u = ed.first, v = ed.second;
    if (a[u] < a[v]) swap(u, v);
    cout << u + 1 << " " << v + 1 << endl;
  }
}
void reset() {
  chert = 0;
  vc.clear();
  e.clear();
  for (int i = 0; i < n; i++) {
    adj[i].clear();
    badj[i].clear();
    mark[i] = false;
  }
}
