#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> dir_graph;   vector<int> vis, order;   void dfs(int i) {     vis[i] = true;     for (int j = 0; j < dir_graph[i].size(); j++) {       if (!vis[dir_graph[i][j]]) {         dfs(dir_graph[i][j]);       }     }     order.push_back(i);   }   int main() {     int cases;     cin >> cases;     while (cases--) {       int n, m;       cin >> n >> m;       dir_graph = vector<vector<int>>(n);       order.clear();       vis = vector<int>(n);       vector<pair<int, int>> edges;       int type, s, d;       for (int i = 0; i < m; i++) {         cin >> type >> s >> d;         s--;         d--;         if (type == 1) dir_graph[s].push_back(d);         edges.push_back({s, d});       }       for (int i = 0; i < n; i++) {         if (!vis[i]) {           dfs(i);         }       }       vector<int> pos(n);       reverse(order.begin(), order.end());       for (int i = 0; i < n; ++i) {         pos[order[i]] = i;       }       bool hasCycle = false;       for (int i = 0; i < n; i++) {         for (int j = 0; j < dir_graph[i].size(); j++) {           if (pos[i] > pos[dir_graph[i][j]]) {             hasCycle = true;             break;           }         }       }       if (hasCycle)         cout <<  NO  << endl;       else {         cout <<  YES n ;         for (int i = 0; i < edges.size(); i++) {           if (pos[edges[i].first] < pos[edges[i].second]) {             cout << edges[i].first + 1 <<     << edges[i].second + 1 << endl;           } else {             cout << edges[i].second + 1 <<     << edges[i].first + 1 << endl;           }         }       }     }     return 0;   }   