#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<pair<int, int>>> adjList;
vector<pair<int, int>> ans;
vector<int> topo, status;
bool cycle = false;
void dfs(int p, int parent) {
  if (cycle) return;
  status[p] = 1;
  for (auto i : adjList[p])
    if (i.second == -1) {
      if (status[i.first] == 0)
        dfs(i.first, p);
      else if (status[i.first] == 1)
        cycle = true;
    }
  status[p] = 2;
  topo.push_back(p);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    cycle = false;
    ans.assign(m, {-1, -1});
    adjList.assign(n, {});
    status.assign(n, 0);
    topo.clear();
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      b -= 1;
      c -= 1;
      if (a == 0) {
        a = i;
      } else {
        a = -1;
        ans[i] = {b, c};
      }
      adjList[b].push_back({c, a});
      if (a != -1) adjList[c].push_back({b, a});
    }
    for (int i = 0; i < n; i++)
      if (status[i] == 0) dfs(i, i);
    if (cycle) {
      cout << "NO"
           << "\n";
      continue;
    }
    cout << "YES"
         << "\n";
    status.assign(n, 0);
    reverse(topo.begin(), topo.end());
    for (auto i : topo)
      for (auto e : adjList[i])
        if (e.second != -1 and ans[e.second].first == -1) {
          ans[e.second] = {i, e.first};
        }
    for (auto i : ans) cout << i.first + 1 << " " << i.second + 1 << "\n";
  }
  return 0;
}
