#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 105;
vector<int> g1[N], g2[N];
int in[N], vis[N];
vector<pair<int, int> > ans;
void solve() {
  int n, m;
  cin >> n >> m;
  ans.clear();
  for (int i = 1; i <= n; ++i) {
    in[i] = vis[i] = 0;
    g1[i].clear();
    g2[i].clear();
  }
  for (int i = 1; i <= m; ++i) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 1) {
      g1[u].push_back(v);
      in[v]++;
    } else {
      g2[u].push_back(v);
      g2[v].push_back(u);
    }
  }
  queue<int> que;
  for (int i = 1; i <= n; ++i) {
    if (in[i] == 0) que.push(i);
  }
  int cnt = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    vis[u] = 1;
    ++cnt;
    for (auto v : g1[u]) {
      in[v]--;
      if (in[v] == 0 && !vis[v]) {
        que.push(v);
      }
      ans.push_back({u, v});
    }
    for (auto v : g2[u]) {
      if (vis[v] == 0) ans.push_back({u, v});
    }
  }
  if (cnt == n) {
    cout << "YES" << endl;
    for (auto it : ans) cout << it.first << " " << it.second << endl;
  } else {
    cout << "NO" << endl;
  }
}
int main() {
  int _ = 1;
  cin >> _;
  for (int i = 1; i <= _; ++i) solve();
}
