#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > adj;
bool acyclic = 1;
int topo[500005];
int ptr = 0;
bool visited[500005];
bool in_stack[500005];
void solve(int i) {
  if (in_stack[i]) {
    acyclic = 0;
    return;
  }
  if (visited[i]) {
    return;
  }
  visited[i] = 1;
  in_stack[i] = 1;
  for (auto m : adj[i]) {
    solve(m);
  }
  in_stack[i] = 0;
  topo[ptr++] = i;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    adj.resize(n + 2);
    for (int(i) = 1; (i) < (n + 2); (i)++) {
      adj[i].clear();
    }
    vector<pair<int, int> > ans;
    for (int(i) = 0; (i) < (m); (i)++) {
      bool dir;
      int u, v;
      cin >> dir >> u >> v;
      if (dir) {
        adj[u].push_back(v);
      }
      if (!dir) {
        ans.push_back(make_pair((u), (v)));
      }
    }
    acyclic = 1;
    ptr = 0;
    memset((visited), 0, sizeof((visited)));
    bool valid = 0;
    for (int i = 1; i <= n; ++i) {
      valid = 1;
      solve(i);
    }
    if (!valid || !acyclic) {
      cout << "NO\n";
      continue;
    }
    unordered_map<int, int> topo_map;
    for (int i = 0; i < n; ++i) {
      topo_map[topo[i]] = i;
    }
    cout << "YES\n";
    for (auto p : ans) {
      if (topo_map[p.first] > topo_map[p.second]) {
        cout << p.first << ' ' << p.second << endl;
      } else {
        cout << p.second << ' ' << p.first << endl;
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (auto m : adj[i]) {
        cout << i << ' ' << m << endl;
      }
    }
  }
}
