#include <bits/stdc++.h>
using namespace std;
vector<int> visit;
vector<int> order;
vector<vector<int>> g;
void dfs(int v) {
  visit[v] = 1;
  for (auto i : g[v]) {
    if (!visit[i]) dfs(i);
  }
  order.push_back(v);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int a1;
  cin >> a1;
  for (int TT = 0; TT < a1; TT++) {
    int n, m;
    cin >> n >> m;
    g = vector<vector<int>>(n + 1);
    vector<pair<int, int>> ed;
    for (int i = 0, t, x, y; i < m; i++) {
      cin >> t >> x >> y;
      if (t) {
        g[x].push_back(y);
      }
      ed.push_back({x, y});
    }
    visit = vector<int>(n + 1);
    for (int i = 1; i <= n; i++) {
      if (!visit[i]) dfs(i);
    }
    reverse(order.begin(), order.end());
    vector<int> pos(n + 1);
    for (int i = 0; i < n; i++) {
      pos[order[i]] = i + 1;
    }
    bool bad = false;
    for (int i = 1; i <= n; i++) {
      for (auto j : g[i]) {
        if (pos[i] > pos[j]) {
          bad = true;
          break;
        }
      }
    }
    if (bad)
      cout << "NO\n";
    else {
      cout << "YES\n";
      for (auto x : ed) {
        if (pos[x.first] < pos[x.second])
          cout << x.first << " " << x.second << "\n";
        else
          cout << x.second << " " << x.first << "\n";
      }
    }
  }
  return 0;
}
