#include <bits/stdc++.h>
using namespace std;
const double Pi = 3.14159265, eps = 1e-6;
const int oo = 1e9, Mod = 1e9 + 7;
const long long ool = 1e18;
int const N = 2e5 + 10, M = N;
int n, m;
vector<vector<int>> g;
pair<int, int> edge[M];
bool direct[M];
bool vis[N];
vector<int> tpl;
bool ext[N];
bool fl;
void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    if (!vis[v])
      dfs(v);
    else if (!ext[v])
      fl = 1;
  }
  ext[u] = 1;
  tpl.push_back(u);
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> m;
    g.clear();
    g.resize(n + 1);
    for (int i = 1; i <= n; i++) ext[i] = vis[i] = 0;
    for (int i = 1; i <= m; i++) {
      cin >> direct[i];
      int u, v;
      cin >> u >> v;
      if (direct[i]) {
        g[u].push_back(v);
      }
      edge[i] = {u, v};
    }
    fl = 0;
    tpl.clear();
    for (int i = 1; i <= n; i++) {
      if (!vis[i]) dfs(i);
    }
    if (fl) {
      cout << "NO\n";
      continue;
    }
    cout << "YES\n";
    reverse(tpl.begin(), tpl.end());
    int ind[n + 1];
    for (int i = 0; i < tpl.size(); i++) ind[tpl[i]] = i + 1;
    for (int i = 1; i <= m; i++) {
      if (!direct[i]) {
        if (ind[edge[i].first] > ind[edge[i].second])
          swap(edge[i].first, edge[i].second);
      }
      cout << edge[i].first << ' ' << edge[i].second << '\n';
    }
  }
}
