#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 1e17;
const long long mod = 1e9 + 7;
long long modInverse(long long a, long long m) {
  long long m0 = m;
  long long y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long q = a / m;
    long long t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
long long powm(long long a, long long b) {
  a = a % mod;
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (res % mod);
}
const int N = 2e5 + 5;
int vis[N];
vector<long long> v;
vector<long long> g[N];
void dfs(int pos) {
  vis[pos] = 1;
  for (auto i : g[pos])
    if (vis[i] == 0) dfs(i);
  v.push_back(pos);
}
vector<pair<long long, long long> > v1, v2;
int bit[N];
vector<pair<long long, long long> > tree[N];
set<pair<long long, long long> > s;
bool bo;
void dfa(long long pos, int t) {
  bit[pos] = t;
  for (pair<long long, long long> i : tree[pos]) {
    if (i.second == 1 &&
        s.find(make_pair(min(i.first, pos), max(i.first, pos))) != s.end()) {
      v1.push_back(make_pair(i.first, pos));
      s.erase(s.find(make_pair(min(i.first, pos), max(i.first, pos))));
      continue;
    }
    if (i.second == 1 &&
        s.find(make_pair(min(i.first, pos), max(i.first, pos))) == s.end()) {
      continue;
    }
    if (i.second == 0 && bit[i.first] == t) {
      bo = true;
      continue;
    }
    if (i.second == 0 && bit[i.first] == 0) {
      dfa(i.first, t);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  v.clear();
  v1.clear();
  v2.clear();
  s.clear();
  bo = false;
  for (int i = 0; i < n; i++) {
    bit[i] = 0;
    tree[i].clear();
  }
  for (int i = 0; i < n; i++) g[i].clear();
  for (int i = 0; i < m; i++) {
    int t, aa, bb;
    cin >> t >> aa >> bb;
    aa--;
    bb--;
    if (t == 0)
      v2.push_back(make_pair(aa, bb));
    else {
      v1.push_back(make_pair(aa, bb));
      g[aa].push_back(bb);
      tree[aa].push_back(make_pair(bb, 0));
    }
  }
  for (pair<long long, long long> i : v2) {
    tree[i.first].push_back(make_pair(i.second, 1));
    tree[i.second].push_back(make_pair(i.first, 1));
    s.insert(make_pair(min(i.first, i.second), max(i.first, i.second)));
  }
  for (int i = 0; i < n; i++) vis[i] = 0;
  for (int i = 0; i < n; i++)
    if (vis[i] == 0) dfs(i);
  for (int i = 0; i < n; i++) vis[i] = 0;
  for (int i = 0; i < n; i++) {
    vis[v[i]] = 1;
    for (int j : g[v[i]])
      if (vis[j] == 0) {
        cout << "NO\n";
        return;
      }
  }
  int t = 1;
  for (int i = 0; i < n; i++) {
    if (bit[i] == 0) {
      dfa(i, t);
      t++;
    }
  }
  cout << "YES\n";
  for (auto i : v1) cout << i.first + 1 << ' ' << i.second + 1 << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
}
