#include <bits/stdc++.h>
using namespace std;
const long long N = 2 * 1e5 + 5;
long long mod = 998244353;
long long n;
vector<long long> adj[N];
long long col[N];
long long parent[N], level[N];
long long visited[N] = {0};
vector<long long> ans;
long long cycle = 0;
long long hash1[N];
void dfs1(long long i, long long p) {
  visited[i] = 1;
  col[i] = 1;
  for (auto u : adj[i]) {
    if (!visited[u]) {
      level[u] = level[i] + 1;
      parent[u] = i;
      dfs1(u, i);
    } else {
      if (u != p && col[u] == 1) {
        cycle = 1;
      }
    }
  }
  col[i] = 2;
  return;
}
void dfs(long long v) {
  visited[v] = true;
  for (auto u : adj[v]) {
    if (!visited[u]) dfs(u);
  }
  ans.push_back(v);
}
void topological_sort() {
  for (long long i = 0; i < n; i++) visited[i] = 0;
  ans.clear();
  for (long long i = 0; i < n; ++i) {
    if (!visited[i]) dfs(i);
  }
  reverse(ans.begin(), ans.end());
}
void cl(long long n) {
  for (long long i = 0; i < n; i++)
    adj[i].clear(), hash1[i] = -1, parent[i] = -1, level[i] = 0, col[i] = 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    cycle = 0;
    long long m;
    cin >> m;
    vector<pair<long long, long long>> edge, edge1;
    for (long long i = 0; i < m; i++) {
      long long ti, x, y;
      cin >> ti >> x >> y;
      x--;
      y--;
      if (ti == 1) {
        adj[x].push_back(y);
        edge1.push_back({x, y});
      } else {
        edge.push_back({x, y});
      }
    }
    for (long long i = 0; i < n; i++) visited[i] = 0;
    for (long long i = 0; i < n; i++) {
      if (!visited[i]) {
        level[i] = 0;
        parent[i] = -1;
        dfs1(i, -1);
        if (cycle) goto pp;
      }
    }
    topological_sort();
    for (long long i = 0; i < n; i++) {
      hash1[ans[i]] = i;
    }
    cout << "YES\n";
    for (long long i = 0; i < edge1.size(); i++) {
      cout << edge1[i].first + 1 << " " << edge1[i].second + 1 << "\n";
    }
    for (long long i = 0; i < edge.size(); i++) {
      long long x = edge[i].first;
      long long y = edge[i].second;
      if (hash1[x] > hash1[y]) {
        cout << y + 1 << " " << x + 1 << "\n";
      } else
        cout << x + 1 << " " << y + 1 << "\n";
    }
    cl(n);
    continue;
  pp:
    cout << "NO\n";
    cl(n);
  }
}
