#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int maxm = 4e5 + 10;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const double pi = acos(-1.0);
int n, m;
int u[maxn], v[maxn], st[maxn];
vector<int> h[maxn];
int cnt[maxn], k;
queue<int> q;
void topsort() {
  for (int i = 1; i <= n; i++)
    if (!st[i]) q.push(i);
  while (q.size()) {
    int u = q.front();
    q.pop();
    cnt[u] = (++k);
    for (int it : h[u]) {
      st[it]--;
      if (!st[it]) q.push(it);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    k = 0;
    for (int i = 1; i <= m; i++) {
      int temp;
      cin >> temp >> u[i] >> v[i];
      if (temp) {
        st[v[i]]++;
        h[u[i]].push_back(v[i]);
      }
    }
    topsort();
    if (n > k)
      cout << "NO" << endl;
    else {
      cout << "YES" << endl;
      for (int i = 1; i <= m; i++)
        if (cnt[u[i]] < cnt[v[i]])
          cout << u[i] << " " << v[i] << endl;
        else
          cout << v[i] << " " << u[i] << endl;
    }
    for (int i = 1; i <= n; i++) {
      cnt[i] = st[i] = 0;
      h[i].clear();
    }
  }
  return ~~(0 - 0);
}
