#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, q, m, in[N], vis[N];
vector<int> adj[N];
set<int> s;
struct edge {
  int u, v;
} ed[N];
void readInput() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) adj[i].clear(), in[i] = 0;
  for (int i = 0; i < m; i++) {
    int u, v;
    bool d;
    cin >> d >> u >> v;
    ed[i].u = --u, ed[i].v = --v;
    if (d) adj[u].push_back(v), in[v]++;
  }
}
void solve() {
  for (int i = 0; i < n; i++)
    if (!in[i]) s.insert(i);
  int num = 0;
  while (!s.empty()) {
    int u = *s.begin();
    s.erase(s.begin());
    vis[u] = num;
    for (auto v : adj[u]) {
      in[v]--;
      if (!in[v]) s.insert(v);
    }
    num++;
  }
  if (num != n) {
    cout << "NO" << endl;
    return;
  }
  cout << "YES" << endl;
  for (int i = 0; i < m; i++)
    if (vis[ed[i].u] < vis[ed[i].v])
      cout << ed[i].u + 1 << ' ' << ed[i].v + 1 << endl;
    else
      cout << ed[i].v + 1 << ' ' << ed[i].u + 1 << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> q;
  while (q--) readInput(), solve();
  return 0;
}
