#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1000000007;
long long int MX = 4000000000000000000;
long long int T;
long long int n, m;
vector<long long int> adj[200200];
long long int ord[200200];
long long int degree[200200];
vector<int> top_order;
bool topsort() {
  top_order.clear();
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < adj[i].size(); j++) degree[adj[i][j]]++;
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (!degree[i]) q.push(i);
  int cnt = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    top_order.push_back(u);
    for (int i = 0; i < adj[u].size(); i++)
      if (--degree[adj[u][i]] == 0) q.push(adj[u][i]);
    cnt++;
  }
  if (cnt != n) {
    return 0;
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      degree[i] = 0;
      adj[i].clear();
      ord[i] = 0;
    }
    long long int t, x, y;
    vector<pair<long long int, long long int> > edges;
    vector<pair<long long int, long long int> > ans;
    for (int i = 1; i <= m; i++) {
      cin >> t >> x >> y;
      if (t == 1) {
        ans.push_back(make_pair(x, y));
        adj[x].push_back(y);
      } else {
        edges.push_back(make_pair(x, y));
      }
    }
    bool res = topsort();
    if (!res) {
      cout << "NO\n";
    } else {
      for (int i = 0; i < top_order.size(); i++) {
        ord[top_order[i]] = i;
      }
      for (int i = 0; i < edges.size(); i++) {
        x = edges[i].first;
        y = edges[i].second;
        if (ord[x] < ord[y]) {
          adj[x].push_back(y);
          ans.push_back(make_pair(x, y));
        } else {
          adj[y].push_back(x);
          ans.push_back(make_pair(y, x));
        }
      }
      res = topsort();
      if (res) {
        cout << "YES\n";
        for (int i = 0; i < m; i++) {
          cout << ans[i].first << " " << ans[i].second << endl;
        }
      } else {
        cout << "NO\n";
      }
    }
  }
  return 0;
}
