#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e9;
const int N = (int)2e5 + 1;
vector<vector<int>> gr;
int vis[N], idx[N], instack[N];
stack<int> st;
void topSort(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  for (auto v : gr[u]) {
    topSort(v);
  }
  st.push(u);
}
bool checkCycle(int u) {
  if (instack[u]) return true;
  if (vis[u]) return false;
  vis[u] = 1;
  instack[u] = 1;
  for (auto v : gr[u]) {
    bool f = checkCycle(v);
    if (f) return true;
  }
  instack[u] = 0;
  return false;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    gr.clear();
    gr.resize(n + 1);
    vector<pair<int, int>> undirEgdes, dirEdges;
    for (int i = 1; i <= n; ++i) vis[i] = 0, instack[i] = 0;
    for (int i = 0; i < m; ++i) {
      int t, u, v;
      cin >> t >> u >> v;
      if (!t)
        undirEgdes.push_back({u, v});
      else {
        gr[u].push_back(v);
        dirEdges.push_back({u, v});
      }
    }
    bool isCyclic = false;
    for (int i = 1; i <= n; ++i) {
      isCyclic = checkCycle(i);
      if (isCyclic) break;
    }
    if (isCyclic)
      puts("NO");
    else {
      for (int i = 1; i <= n; ++i) vis[i] = 0, instack[i] = 0;
      for (int i = 1; i <= n; ++i) {
        if (!vis[i]) topSort(i);
      }
      int i = 1;
      while (!st.empty()) {
        idx[st.top()] = i++;
        st.pop();
      }
      puts("YES");
      for (auto x : undirEgdes) {
        if (idx[x.first] < idx[x.second])
          cout << x.first << " " << x.second << endl;
        else
          cout << x.second << " " << x.first << endl;
      }
      for (auto x : dirEdges) {
        cout << x.first << " " << x.second << endl;
      }
    }
  }
}
