#include <bits/stdc++.h>
using namespace std;
template <class T, class V>
ostream& operator<<(ostream& s, pair<T, V> a) {
  s << a.first << ' ' << a.second;
  return s;
}
vector<long long int> adj[200005];
int timer = 0;
int topo[200005], vis[200005];
bool dfs(int ind) {
  vis[ind] = 1;
  for (auto x : adj[ind]) {
    if (vis[x] == 1) return false;
    if (vis[x] == 0 && !dfs(x)) return false;
  }
  vis[ind] = 2;
  topo[ind] = timer++;
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  cin >> T;
  for (int qq = 1; qq <= T; qq++) {
    long long int n, m;
    cin >> n >> m;
    vector<pair<long long int, long long int> > ans;
    vector<pair<long long int, long long int> > rem;
    for (int i = 1; i <= n; i++) adj[i].clear(), vis[i] = 0;
    timer = 0;
    for (int i = 0; i < m; i++) {
      int t, u, v;
      cin >> t >> u >> v;
      if (t)
        adj[u].push_back(v),
            ans.push_back(pair<long long int, long long int>(u, v));
      else
        rem.push_back(pair<long long int, long long int>(u, v));
    }
    bool ok = true;
    for (int i = 1; i <= n && ok; i++) {
      if (vis[i] == 0) ok &= dfs(i);
    }
    if (!ok)
      cout << "NO" << endl;
    else {
      while (!rem.empty()) {
        pair<long long int, long long int> a = rem.back();
        rem.pop_back();
        if (topo[a.first] < topo[a.second]) swap(a.first, a.second);
        ans.push_back(a);
      }
      cout << "YES" << endl;
      for (auto x : ans) cout << x << endl;
    }
  }
}
