#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 10;
const long long inf = 1e18 + 100;
const long long mod = 1e9 + 7;
vector<vector<long long>> grp;
vector<long long> order;
vector<long long> vis;
void dfs(int node) {
  if (vis[node]) {
    return;
  }
  vis[node] = 1;
  for (auto i : grp[node]) {
    dfs(i);
  }
  order.push_back(node);
}
void solve() {
  long long n, m;
  cin >> n >> m;
  grp = vector<vector<long long>>(n + 1);
  order.clear();
  vis = vector<long long>(n + 1, 0);
  vector<pair<long long, long long>> eds;
  for (int i = 0; i < m; i++) {
    long long t, u, v;
    cin >> t >> u >> v;
    if (t) {
      grp[u - 1].push_back(v - 1);
    }
    eds.push_back(make_pair(u - 1, v - 1));
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) dfs(i);
  }
  reverse(order.begin(), order.end());
  vector<int> pos(n);
  for (int i = 0; i < n; i++) {
    pos[order[i]] = i;
  }
  bool fl = true;
  for (int i = 0; i < n; i++) {
    for (auto j : grp[i]) {
      if (pos[i] > pos[j]) {
        fl = false;
      }
    }
  }
  if (!fl) {
    cout << "NO\n";
  } else {
    cout << "YES\n";
    for (auto i : eds) {
      if (pos[i.first] < pos[i.second]) {
        cout << i.first + 1 << " " << i.second + 1 << "\n";
      } else {
        cout << i.second + 1 << " " << i.first + 1 << "\n";
      }
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
