#include <bits/stdc++.h>
using namespace std;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
vector<vector<int> > adjL;
vector<int> vis, topo;
void Toposort(int u) {
  vis[u] = 1;
  for (auto& v : adjL[u])
    if (!vis[v]) Toposort(v);
  topo.push_back(u);
}
void test_case() {
  int n, m;
  cin >> n >> m;
  adjL = vector<vector<int> >(n + 1), vis = vector<int>(n + 1), topo.clear();
  vector<pair<int, int> > edges;
  for (int i = 0; i < m; i++) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t) adjL[u].push_back(v);
    edges.push_back({u, v});
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) Toposort(i);
  vector<int> idx(n + 1);
  for (int i = 0; i < n; i++) idx[topo[i]] = i;
  for (int u = 1; u <= n; u++) {
    for (auto& v : adjL[u]) {
      if (idx[u] < idx[v]) {
        cout << "NO\n";
        return;
      }
    }
  }
  cout << "YES\n";
  for (auto& it : edges) {
    if (idx[it.first] < idx[it.second]) swap(it.first, it.second);
    cout << it.first << ' ' << it.second << "\n";
  }
}
int main() {
  file();
  fast();
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}
