#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>>& asd, vector<int>& sorted_nodes,
         vector<int>& state, int node);
bool has_cycle = false;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (unsigned long long i = 0; i < q; i++) {
    has_cycle = false;
    int n, m;
    cin >> n >> m;
    vector<vector<int>> asd(n);
    vector<int> state(n);
    vector<int> sorted_nodes;
    vector<pair<int, int>> undirected_edges;
    int type, a, b;
    for (unsigned long long j = 0; j < m; j++) {
      cin >> type >> a >> b;
      a--;
      b--;
      if (type == 0) {
        undirected_edges.push_back({a, b});
      } else {
        asd[a].push_back(b);
      }
    }
    for (int j = 0; j < n; j++) {
      if (state[j] != 2) {
        dfs(asd, sorted_nodes, state, j);
      }
    }
    if (has_cycle) {
      cout << "NO\n";
    } else {
      reverse(sorted_nodes.begin(), sorted_nodes.end());
      vector<int> depth(n, -1);
      for (int j = 0; j < sorted_nodes.size(); j++) {
        depth[sorted_nodes[j]] = j;
      }
      cout << "YES\n";
      for (auto edge : undirected_edges) {
        a = edge.first;
        b = edge.second;
        if (depth[a] < depth[b]) {
          cout << a + 1 << ' ' << b + 1 << '\n';
        } else {
          cout << b + 1 << ' ' << a + 1 << '\n';
        }
      }
      for (int j = 0; j < n; j++) {
        for (auto k : asd[j]) {
          cout << j + 1 << ' ' << k + 1 << '\n';
        }
      }
    }
  }
}
void dfs(vector<vector<int>>& asd, vector<int>& sorted_nodes,
         vector<int>& state, int node) {
  if (has_cycle) return;
  if (state[node] == 1) {
    has_cycle = true;
    return;
  }
  if (state[node] == 2) return;
  state[node] = 1;
  for (int nextnode : asd[node]) {
    dfs(asd, sorted_nodes, state, nextnode);
  }
  state[node] = 2;
  sorted_nodes.push_back(node);
}
