#include <bits/stdc++.h>
using namespace std;
void start() {}
vector<long long> top_order, pos(200005);
class Graph {
  long long V;
  list<long long>* adj;

 public:
  Graph(long long V);
  void addEdge(long long u, long long v);
  bool topologicalSort();
};
Graph::Graph(long long V) {
  this->V = V;
  adj = new list<long long>[V];
}
void Graph::addEdge(long long u, long long v) { adj[u].push_back(v); }
bool Graph::topologicalSort() {
  vector<long long> in_degree(V, 0);
  for (long long u = 0; u < V; u++) {
    list<long long>::iterator itr;
    for (itr = adj[u].begin(); itr != adj[u].end(); itr++) in_degree[*itr]++;
  }
  queue<long long> q;
  for (long long i = 0; i < V; i++)
    if (in_degree[i] == 0) q.push(i);
  long long cnt = 0;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    top_order.push_back(u);
    cnt++;
    pos[u] = cnt;
    list<long long>::iterator itr;
    for (itr = adj[u].begin(); itr != adj[u].end(); itr++)
      if (--in_degree[*itr] == 0) q.push(*itr);
  }
  if (cnt != V) {
    return 0;
  }
  return 1;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    top_order.clear();
    pos.clear();
    long long n, m;
    cin >> n >> m;
    Graph g(n + 1);
    vector<pair<long long, pair<long long, long long>>> v;
    for (long long i = 0; i < m; i++) {
      long long x, y, z;
      cin >> x >> y >> z;
      v.push_back({x, {y, z}});
      if (x == 1) g.addEdge(y, z);
    }
    if (!g.topologicalSort()) {
      cout << "NO" << endl;
    } else {
      cout << "YES" << endl;
      for (long long i = 0; i < m; i++) {
        if (v[i].first == 1)
          cout << v[i].second.first << " " << v[i].second.second << endl;
        else {
          if (pos[v[i].second.first] > pos[v[i].second.second])
            cout << v[i].second.second << " " << v[i].second.first << endl;
          else
            cout << v[i].second.first << " " << v[i].second.second << endl;
        }
      }
    }
  }
}
