#include <bits/stdc++.h>
using namespace std;
long long n, m, dad[200005], cyc, ord[200005], ty[200005], x[200005], y[200005];
vector<long long> v[200005];
bool vis[200005], current[200005];
queue<long long> q;
void dfs(int ver) {
  if (current[ver]) {
    cyc = 1;
    return;
  }
  if (vis[ver]) return;
  vis[ver] = 1;
  current[ver] = 1;
  for (int i = 0; i < v[ver].size(); i++) {
    dfs(v[ver][i]);
  }
  current[ver] = 0;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      cin >> ty[i] >> x[i] >> y[i];
      if (ty[i] == 1) {
        v[x[i]].push_back(y[i]);
        dad[y[i]]++;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      dfs(i);
    }
    if (cyc) {
      cout << "NO" << endl;
    } else {
      cout << "YES" << endl;
      for (int i = 1; i <= n; i++) {
        if (dad[i] == 0) q.push(i);
      }
      int c = 0;
      while (!q.empty()) {
        long long p = q.front();
        q.pop();
        ord[p] = c++;
        for (int i = 0; i < v[p].size(); i++) {
          dad[v[p][i]]--;
          if (dad[v[p][i]] == 0) q.push(v[p][i]);
        }
      }
      for (int i = 0; i < m; i++) {
        if (ty[i] == 1)
          cout << x[i] << ' ' << y[i] << endl;
        else {
          if (ord[x[i]] < ord[y[i]])
            cout << x[i] << ' ' << y[i] << endl;
          else
            cout << y[i] << ' ' << x[i] << endl;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      v[i].clear();
      vis[i] = 0;
      current[i] = 0;
      cyc = 0;
      ord[i] = 0;
      dad[i] = 0;
    }
  }
  return 0;
}
