#include <bits/stdc++.h>
using namespace std;
vector<long long int> g[200001];
vector<long long int> g1[200001];
vector<long long int> vec;
bool vis[200001];
void fun(long long int u) {
  vis[u] = 1;
  for (long long int i = 0; i < g[u].size(); i++) {
    long long int v = g[u][i];
    if (!vis[v]) {
      fun(v);
    }
  }
  vec.push_back(u);
}
bool st[200001];
bool foo = 1;
bool check(long long int u) {
  if (!foo) return false;
  st[u] = 1;
  vis[u] = 1;
  for (long long int i = 0; i < g[u].size(); i++) {
    long long int v = g[u][i];
    if (st[v]) {
      foo = 0;
      return false;
    }
    if (!vis[v]) check(v);
  }
  st[u] = 0;
  return true;
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    for (long long int i = 1; i <= n; i++) {
      g[i].clear();
      g1[i].clear();
    }
    vec.clear();
    memset(vis, false, sizeof(vis));
    memset(st, 0, sizeof(st));
    for (long long int i = 1; i <= m; i++) {
      long long int k, u, v;
      cin >> k >> u >> v;
      if (k == 1) {
        g[u].push_back(v);
      } else {
        g1[u].push_back(v);
        g1[v].push_back(u);
      }
    }
    bool ans = 1;
    foo = 1;
    for (long long int i = 1; i <= n; i++) {
      if (!vis[i]) {
        check(i);
        if (!foo) {
          ans = 0;
          break;
        }
      }
    }
    if (!ans) {
      cout << "NO\n";
      continue;
    }
    memset(vis, 0, sizeof(vis));
    for (long long int i = 1; i <= n; i++) {
      if (!vis[i]) fun(i);
    }
    reverse(vec.begin(), vec.end());
    map<pair<long long int, long long int>, bool> done;
    for (long long int i = 0; i < vec.size(); i++) {
      long long int u = vec[i];
      for (long long int j = 0; j < g1[u].size(); j++) {
        long long int v = g1[u][j];
        long long int u1 = min(u, v), v1 = max(u, v);
        if (!done[{u1, v1}]) {
          done[{u1, v1}] = 1;
          g[u].push_back(v);
        }
      }
    }
    cout << "YES\n";
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j < g[i].size(); j++) {
        cout << i << " " << g[i][j] << "\n";
      }
    }
  }
  return 0;
}
