#include <bits/stdc++.h>
using namespace std;
void dfs1(int v, bool &cy, vector<vector<int>> &g, vector<int> &used,
          vector<int> &order) {
  used[v] = 1;
  for (int to : g[v]) {
    if (used[to] == 1) {
      cy = 1;
      continue;
    } else if (used[to] == 2)
      continue;
    dfs1(to, cy, g, used, order);
  }
  used[v] = 2;
  order.push_back(v);
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> go(n), gn(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    b--;
    c--;
    if (a == 1) {
      go[b].push_back(c);
    } else {
      gn[b].push_back(c);
      gn[c].push_back(b);
    }
  }
  vector<int> used(n, 0), order;
  bool cy = 0;
  for (int i = 0; i < n; i++)
    if (!used[i]) dfs1(i, cy, go, used, order);
  if (cy) {
    cout << "NO\n";
    return;
  }
  for (int i = 0; i < n; i++) used[i] = 0;
  cout << "YES\n";
  for (int i = n - 1; i >= 0; i--) {
    int v = order[i];
    for (int to : gn[v]) {
      if (!used[to]) cout << v + 1 << ' ' << to + 1 << '\n';
    }
    for (int to : go[v]) cout << v + 1 << ' ' << to + 1 << '\n';
    used[v] = 1;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  cin >> q;
  for (; q > 0; q--) {
    solve();
  }
}
