#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
using v2 = vector<vector<T>>;
template <typename T>
inline v2<T> init(int r, int c, const T& t) {
  return v2<T>(r, vector<T>(c, t));
}
int n, m;
vector<set<pair<int, bool>>> adj;
vector<bool> v, act;
bool bad;
void dfs1(int node) {
  v[node] = true;
  act[node] = true;
  for (auto& a : adj[node]) {
    if (!a.second) continue;
    int next = a.first;
    if (adj[next].count({node, 1})) continue;
    if (!v[next]) {
      dfs1(next);
    } else if (act[next]) {
      bad = true;
    }
  }
  act[node] = false;
}
void dfs2(int node) {
  v[node] = true;
  for (auto& a : adj[node]) {
    int next = a.first;
    if (!v[next]) {
      if (!a.second) {
        if (adj[next].count({node, 1})) {
          dfs2(next);
        }
      }
    }
  }
  for (auto& a : adj[node]) {
    int next = a.first;
    if (a.second && adj[next].count({node, 1})) {
      adj[next].erase(adj[next].find({node, 1}));
    }
  }
}
void solve() {
  cin >> n >> m;
  adj = vector<set<pair<int, bool>>>(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --b, --c;
    if (!a) {
      adj[b].insert({c, true});
      adj[c].insert({b, true});
    } else {
      adj[b].insert({c, true});
      adj[c].insert({b, false});
    }
  }
  bad = false;
  v = vector<bool>(n, false), act = vector<bool>(n, false);
  for (int i = 0; i < n; ++i) {
    if (!v[i]) dfs1(i);
  }
  if (bad) {
    cout << "NO\n";
    return;
  }
  v = vector<bool>(n, false);
  for (int i = 0; i < n; ++i) {
    if (!v[i]) dfs2(i);
  }
  cout << "YES\n";
  for (int i = 0; i < n; ++i) {
    for (auto& a : adj[i]) {
      if (!a.second) continue;
      int j = a.first;
      cout << i + 1 << " " << j + 1 << '\n';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i) {
    solve();
  }
  return 0;
}
