#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int tt, n, m, p[200005];
vector<int> g[200005], v;
bool used[200005], x, cyc;
pair<int, int> edg[200005];
void topsort(int u) {
  used[u] = true;
  for (int i : g[u])
    if (!used[i]) topsort(i);
  v.push_back(u);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> tt;
  while (tt--) {
    v.clear();
    cyc = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      used[i] = false;
      g[i].clear();
    }
    for (int i = 0; i < m; i++) {
      cin >> x >> edg[i].first >> edg[i].second;
      if (x) g[edg[i].first].push_back(edg[i].second);
    }
    for (int i = 1; i <= n; i++)
      if (!used[i]) topsort(i);
    for (int i = 0; i < n; i++) p[v[i]] = n - i - 1;
    for (int i = 1; i <= n; i++)
      for (int u : g[i])
        if (p[u] < p[i]) cyc = 1;
    if (cyc) {
      cout << "NO\n";
      continue;
    }
    cout << "YES\n";
    for (int i = 0; i < m; i++) {
      if (p[edg[i].first] < p[edg[i].second])
        cout << edg[i].first << ' ' << edg[i].second << '\n';
      else
        cout << edg[i].second << ' ' << edg[i].first << '\n';
    }
  }
  return 0;
}
