#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 2e5 + 10;
vector<long long> g[N];
vector<long long> order;
long long n, m, indeg[N], pos[N], uu[N], vv[N], dir[N];
long long topological() {
  order.clear();
  queue<long long> q;
  for (long long i = 1; i <= n; i++) {
    indeg[i] = 0;
    pos[i] = 0;
  }
  for (long long i = 1; i <= n; i++) {
    for (auto it : g[i]) {
      indeg[it]++;
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (!indeg[i]) q.push(i);
  }
  long long idx = 0;
  while (!q.empty()) {
    long long u = q.front();
    order.push_back(u);
    q.pop();
    idx++;
    pos[u] = idx;
    for (auto v : g[u]) {
      indeg[v]--;
      if (!indeg[v]) q.push(v);
    }
  }
  long long s = order.size();
  if (s == n)
    return 1;
  else
    return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (long long i = 1; i <= n; i++) {
      g[i].clear();
    }
    for (long long i = 1; i <= m; i++) {
      cin >> dir[i] >> uu[i] >> vv[i];
      if (dir[i]) {
        g[uu[i]].push_back(vv[i]);
      }
    }
    if (!topological()) {
      cout << "NO" << endl;
      continue;
    } else {
      cout << "YES" << endl;
      for (long long i = 1; i <= m; i++) {
        if (!dir[i]) {
          if (pos[uu[i]] > pos[vv[i]]) swap(uu[i], vv[i]);
        }
        cout << uu[i] << " " << vv[i] << endl;
      }
    }
  }
  return 0;
}
