#include <bits/stdc++.h>
using namespace std;
vector<long long> ord, used;
vector<vector<long long>> graph;
void dfs(long long u) {
  used[u] = 1;
  for (auto v : graph[u]) {
    if (!used[v]) dfs(v);
  }
  ord.push_back(u);
}
void solve() {
  long long n, m;
  cin >> n >> m;
  graph = vector<vector<long long>>(n);
  vector<pair<long long, long long>> edge;
  for (long long i = 0; i < m; ++i) {
    long long t, x, y;
    cin >> t >> x >> y;
    x--, y--;
    if (t == 1) graph[x].push_back(y);
    edge.push_back(make_pair(x, y));
  }
  ord.clear();
  used = vector<long long>(n);
  for (long long i = 0; i < n; ++i) {
    if (!used[i]) dfs(i);
  }
  vector<long long> pos(n);
  reverse(ord.begin(), ord.end());
  for (long long i = 0; i < n; ++i) {
    pos[ord[i]] = i;
  }
  bool flag = 0;
  for (long long i = 0; i < n; ++i) {
    for (auto j : graph[i]) {
      if (pos[i] > pos[j]) flag = 1;
    }
  }
  if (flag)
    cout << "NO\n";
  else {
    cout << "YES\n";
    for (auto [x, y] : edge) {
      if (pos[x] < pos[y])
        cout << x + 1 << ' ' << y + 1 << '\n';
      else
        cout << y + 1 << ' ' << x + 1 << '\n';
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) solve();
}
