#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<vector<long long>> g;
vector<long long> vis;
vector<long long> ord;
void dfs(long long x) {
  vis[x] = 1;
  for (auto it : g[x]) {
    if (!vis[it]) dfs(it);
  }
  ord.push_back(x);
}
void solve() {
  long long n, m, i;
  i = 0;
  cin >> n >> m;
  g = vector<vector<long long>>(n);
  vector<pair<long long, long long>> edge;
  for (long long i = 0; i < m; ++i) {
    long long t, x, y;
    cin >> t >> x >> y;
    --x, --y;
    if (t == 1) {
      g[x].push_back(y);
    }
    edge.push_back({x, y});
  }
  ord.clear();
  vis = vector<long long>(n);
  for (long long i = 0; i < n; ++i) {
    if (!vis[i]) dfs(i);
  }
  reverse(ord.begin(), ord.end());
  vector<long long> pos(n);
  for (long long i = 0; i < n; ++i) {
    pos[ord[i]] = i;
  }
  bool ok = false;
  for (long long i = 0; i < n; ++i) {
    for (auto j : g[i]) {
      if (pos[i] > pos[j]) ok = true;
    }
  }
  if (ok) {
    cout << "NO"
         << "\n";
  } else {
    cout << "YES"
         << "\n";
    for (auto [x, y] : edge) {
      if (pos[x] < pos[y])
        cout << x + 1 << " " << y + 1 << "\n";
      else
        cout << y + 1 << " " << x + 1 << "\n";
    }
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
