#include <bits/stdc++.h>
using namespace std;
vector<long long int> d_graph[200007], trop_sort;
bool vis[200007];
void dfs(long long int u) {
  vis[u] = 1;
  for (long long int i = 0; i < d_graph[u].size(); i++) {
    long long int v = d_graph[u][i];
    if (!vis[v]) {
      dfs(v);
    }
  }
  trop_sort.push_back(u);
}
int main() {
  long long int t;
  cin >> t;
read:
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    vector<pair<long long int, long long int>> x;
    long long int pos[n + 1];
    trop_sort.clear();
    for (long long int i = 1; i <= n; i++) {
      d_graph[i].clear();
      vis[i] = 0;
    }
    for (long long int i = 1; i <= m; i++) {
      long long int t, u, v;
      cin >> t >> u >> v;
      if (t)
        d_graph[u].push_back(v);
      else
        x.push_back(make_pair(u, v));
    }
    for (long long int i = 1; i <= n; i++) {
      if (!vis[i]) dfs(i);
    }
    for (long long int i = 0; i < n; i++) {
      pos[trop_sort[i]] = i;
    }
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j < d_graph[i].size(); j++) {
        long long int u = i, v = d_graph[i][j];
        if (pos[v] > pos[u]) {
          cout << "NO" << endl;
          goto read;
        }
      }
    }
    for (long long int i = 0; i < x.size(); i++) {
      long long int u = x[i].first, v = x[i].second;
      if (pos[u] > pos[v])
        d_graph[u].push_back(v);
      else
        d_graph[v].push_back(u);
    }
    cout << "YES" << endl;
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j < d_graph[i].size(); j++) {
        long long int u = i, v = d_graph[i][j];
        cout << u << " " << v << endl;
      }
    }
  }
  return 0;
}
