#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
vector<int> d[MAXN], topo;
vector<pair<int, int>> ud;
bool visited[MAXN], cycle;
int idx[MAXN], start[MAXN], finish[MAXN], timer;
void dfs(int node) {
  if (visited[node]) {
    if (!finish[node]) cycle = 1;
    return;
  }
  start[node] = timer++;
  visited[node] = 1;
  for (auto i : d[node]) dfs(i);
  topo.push_back(node);
  finish[node] = timer++;
}
int main() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t, n, m;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0, Case, fr, to; i < m; i++) {
      cin >> Case >> fr >> to;
      if (Case)
        d[fr].push_back(to);
      else
        ud.push_back({fr, to});
    }
    for (int i = 1; i <= n && !cycle; i++)
      if (!visited[i]) dfs(i);
    if (cycle)
      cout << "NO\n";
    else {
      cout << "YES\n";
      for (int i = 0; i < n; i++) idx[topo[n - i - 1]] = i;
      for (int i = 1; i <= n; i++)
        for (auto j : d[i]) cout << i << ' ' << j << endl;
      for (auto i : ud) {
        if (idx[i.first] < idx[i.second])
          cout << i.first << ' ' << i.second << endl;
        else
          cout << i.second << ' ' << i.first << endl;
      }
    }
    for (int i = 1; i <= n; i++)
      visited[i] = 0, d[i].clear(), start[i] = 0, finish[i] = 0;
    ud.clear(), topo.clear();
    timer = 0, cycle = 0;
  }
  return 0;
}
