#include <bits/stdc++.h>
using namespace std;
bool top_sort(int i, vector<vector<int>> &g, vector<int> &v, stack<int> &s,
              vector<int> &rec_stk) {
  if (v[i] == 1) {
    for (int j = 0; j < rec_stk.size(); j++) {
      if (rec_stk[j] == i) return false;
    }
    return true;
  }
  v[i] = 1;
  rec_stk.push_back(i);
  for (int j = 0; j < g[i].size(); j++) {
    bool val = top_sort(g[i][j], g, v, s, rec_stk);
    if (val == false) return false;
  }
  rec_stk.pop_back();
  s.push(i);
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> e(m, vector<int>(3, 0));
    for (int i = 0; i < m; i++) {
      int t1, a, b;
      cin >> t1 >> a >> b;
      e[i][0] = t1;
      e[i][1] = a - 1;
      e[i][2] = b - 1;
    }
    vector<vector<int>> g_un(n);
    vector<vector<int>> g_dir(n);
    for (int i = 0; i < m; i++) {
      if (e[i][0] == 1) {
        g_dir[e[i][1]].push_back(e[i][2]);
      } else {
        g_un[e[i][1]].push_back(e[i][2]);
        g_un[e[i][2]].push_back(e[i][1]);
      }
    }
    stack<int> s;
    vector<int> vis(n, 0), rec_stk;
    bool val = true;
    for (int i = 0; i < n; i++) {
      val = top_sort(i, g_dir, vis, s, rec_stk);
      if (val == false) break;
    }
    if (val == false) {
      cout << "NO" << endl;
      continue;
    }
    while (!s.empty()) {
      int i = s.top();
      s.pop();
      vis[i] = 0;
      for (int j = 0; j < g_un[i].size(); j++) {
        if (vis[g_un[i][j]] == 1) {
          g_dir[i].push_back(g_un[i][j]);
        }
      }
    }
    cout << "YES" << endl;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < g_dir[i].size(); j++) {
        cout << i + 1 << " " << g_dir[i][j] + 1 << endl;
      }
    }
  }
  return 0;
}
