#include <bits/stdc++.h>
using namespace std;
long double PI = 3.14159265359;
long long inf = 1000000000000000007;
long long mod = 1000000007;
long long mod1 = 998244353;
const bool multi = 1;
vector<long long> G[200007];
vector<long long> RG[200007];
vector<pair<long long, long long>> UG[200007];
long long ile[200007];
long long odw[200007];
long long odw1[200007];
long long spojna[200007];
pair<long long, long long> ans[200007];
long long c = 1, counter = 1;
bool ok = 1;
deque<long long> kolejka;
vector<long long> stos;
long long q;
void dfs(long long v) {
  odw[v] = 1;
  for (auto u : RG[v]) {
    if (!odw[u]) dfs(u);
  }
  stos.push_back(v);
}
void dfs1(long long v) {
  spojna[v] = counter;
  q++;
  for (auto u : G[v]) {
    if (spojna[u] == 0) {
      dfs1(u);
    }
  }
}
void clean(long long n) {
  for (long long i = 0; i < n + 4; i++) {
    G[i].clear();
    RG[i].clear();
    UG[i].clear();
    odw[i] = 0;
    odw1[i] = 0;
    spojna[i] = 0;
    ans[i] = {0, 0};
    ile[i] = 0;
    c = 1;
    counter = 1;
    ok = 1;
    stos.clear();
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt;
  if (multi)
    cin >> tt;
  else
    tt = 1;
  while (tt--) {
    long long n, m, a, b, y = 1;
    bool x;
    cin >> n >> m;
    for (long long i = 0; i < m; i++) {
      cin >> x >> a >> b;
      if (x) {
        G[a].push_back(b);
        RG[b].push_back(a);
        ile[b]++;
        ans[i] = {a, b};
      } else {
        UG[a].push_back({b, i});
        UG[b].push_back({a, i});
      }
    }
    for (long long i = 1; i <= n; i++) {
      if (!odw[i]) dfs(i);
    }
    for (long long i = n - 1; i >= 0; i--) {
      if (spojna[stos[i]] == 0) {
        q = 0;
        dfs1(stos[i]);
        if (q > 1) ok = 0;
        counter++;
      }
    }
    for (long long i = 1; i <= n; i++) {
      if (ile[i] == 0) kolejka.push_back(i);
    }
    while (!kolejka.empty()) {
      long long v = kolejka[0];
      kolejka.pop_front();
      odw1[v] = 1;
      for (auto u : G[v]) {
        ile[u]--;
        if (ile[u] == 0) kolejka.push_back(u);
      }
      for (auto u : UG[v]) {
        if (!odw1[u.first]) {
          ans[u.second] = {v, u.first};
        }
      }
    }
    if (!ok)
      cout << "NO" << '\n';
    else {
      cout << "YES" << '\n';
      for (long long i = 0; i < m; i++) {
        cout << ans[i].first << " " << ans[i].second << '\n';
      }
    }
    clean(max(n, m));
  }
  return 0;
}
