#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<int> di[200005];
int a[200005], b[200005];
int in[200005], tp[200005];
int vis[200005];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    int n, m;
    cin >> n >> m;
    int cnt = 0;
    for (int j = 1; j <= n; j++) {
      di[j].clear();
      in[j] = tp[j] = vis[j] = 0;
    }
    for (int j = 0; j < m; j++) {
      int t, x, y;
      cin >> t >> x >> y;
      if (t == 1) {
        in[y]++;
        di[x].push_back(y);
      } else {
        a[cnt] = x;
        b[cnt++] = y;
      }
    }
    queue<int> q;
    int idx = 0;
    for (int j = 1; j <= n; j++) {
      if (in[j] == 0) q.push(j);
    }
    int flag = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      tp[u] = ++idx;
      for (int j = 0; j < di[u].size(); j++) {
        in[di[u][j]]--;
        if (in[di[u][j]] == 0) q.push(di[u][j]);
      }
    }
    for (int j = 1; j <= n; j++) {
      if (!tp[j]) flag = 1;
    }
    if (flag)
      cout << "NO" << endl;
    else {
      cout << "YES" << endl;
      for (int j = 1; j <= n; j++) {
        for (int k = 0; k < di[j].size(); k++) {
          cout << j << ' ' << di[j][k] << endl;
        }
      }
      for (int j = 0; j < cnt; j++) {
        if (tp[a[j]] < tp[b[j]])
          cout << a[j] << ' ' << b[j] << endl;
        else
          cout << b[j] << ' ' << a[j] << endl;
      }
    }
  }
}
