#include <bits/stdc++.h>
using namespace std;
const int N = 200023;
long long int power(long long int a, long long int b) {
  long long int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % 1000000007;
    b /= 2;
    a = (a * a) % 1000000007;
  }
  return ans;
}
bool mysort(const pair<long long int, long long int> &a,
            const pair<long long int, long long int> &b) {
  if (a.first < b.first)
    return 1;
  else if (a.first == b.first)
    return (a.second < b.second);
  else
    return 0;
}
vector<long long int> adj[N];
void solve() {
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) adj[i].clear();
  vector<pair<long long int, long long int>> undir, dir;
  long long int indeg[n + 1];
  memset(indeg, 0, sizeof(indeg));
  long long int ts[n + 1];
  for (long long int i = 0; i < m; i++) {
    long long int t, x, y;
    cin >> t >> x >> y;
    if (t) {
      dir.push_back({x, y});
      adj[x].push_back(y);
      indeg[y]++;
    } else
      undir.push_back({x, y});
  }
  queue<long long int> q;
  long long int flg = 0, curr = 1;
  for (long long int i = 1; i <= n; i++) {
    if (indeg[i] == 0) {
      flg = 1;
      q.push(i);
    }
  }
  if (!flg) {
    cout << "NO" << endl;
    return;
  } else {
    while (!q.empty()) {
      long long int i = q.front();
      ts[i] = curr;
      curr++;
      q.pop();
      for (auto x : adj[i]) {
        indeg[x]--;
        if (!indeg[x]) q.push(x);
      }
    }
  }
  for (long long int i = 1; i <= n; i++) {
    for (auto x : adj[i]) {
      if (ts[x] < ts[i]) {
        cout << "NO" << endl;
        return;
      }
    }
  }
  cout << "YES" << endl;
  for (long long int i = 0; i < undir.size(); i++) {
    if (ts[undir[i].first] > ts[undir[i].second]) {
      swap(undir[i].second, undir[i].first);
    }
    cout << undir[i].first << " " << undir[i].second << endl;
  }
  for (long long int i = 0; i < dir.size(); i++) {
    cout << dir[i].first << " " << dir[i].second << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int Test;
  Test = 1;
  cin >> Test;
  long long int t = 0;
  while (t++ < Test) {
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}
