#include <bits/stdc++.h>
using namespace std;
void fst() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
int mod = 1e9 + 7;
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a;
      res %= mod;
    }
    a = a * a;
    a %= mod;
    b >>= 1;
    b %= mod;
  }
  return res;
}
long long n, k, m, tc, x, y;
int32_t main() {
  fst();
  cin >> tc;
  while (tc--) {
    cin >> n >> m;
    vector<long long> adj[n + 5];
    vector<pair<long long, long long> > undir;
    long long fl = 0;
    vector<long long> indeg(n + 5, 0);
    vector<bool> vis(n + 5, 0);
    for (long long i = 0; i < m; i++) {
      long long t;
      cin >> t;
      cin >> x >> y;
      if (!t) {
        undir.push_back({x, y});
      } else {
        adj[x].push_back(y);
        indeg[y]++;
      }
    }
    queue<long long> q;
    for (long long i = 1; i <= n; i++) {
      if (!indeg[i]) {
        q.push(i);
      }
    }
    vector<long long> id(n + 1);
    long long it = 0;
    while (q.size() != 0) {
      long long s = q.front();
      q.pop();
      id[s] = it++;
      for (auto j : adj[s]) {
        if (--indeg[j] == 0) {
          q.push(j);
        }
      }
    }
    cerr << "it"
         << "=" << it << "\n";
    ;
    if (it != n) {
      cout << "NO\n";
      continue;
    }
    cout << "YES\n";
    for (auto [x, y] : undir) {
      if (id[x] < id[y]) {
        cout << x << " " << y << "\n";
      } else {
        cout << y << " " << x << "\n";
      }
    }
    for (long long i = 1; i <= n + 1; i++) {
      for (auto j : adj[i]) cout << i << " " << j << "\n";
    }
  }
}
