#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1000000000000000000;
const long double pi = 3.14159265358979323846264338;
const long long mod = 1000000007;
long long int pow(long long int x, unsigned long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int invmod(long long int a, long long int m) {
  return pow(a, m - 2, m);
}
vector<vector<long long int>> adj;
vector<long long int> rec, vis, tout;
long long int T = 0, flag = 1;
void dfs(long long int st) {
  rec[st] = 1;
  vis[st] = 1;
  for (long long int x : adj[st]) {
    if (rec[x]) {
      flag = 0;
      return;
    }
    if (!vis[x]) dfs(x);
  }
  rec[st] = 0;
  tout[st] = ++T;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    T = 0, flag = 1;
    adj.assign(n + 1, {});
    rec.assign(n + 1, {});
    vis.assign(n + 1, {});
    tout.assign(n + 1, {});
    vector<pair<long long int, long long int>> p;
    for (long long int i = 0; i < m; i++) {
      long long int a, x, y;
      cin >> a >> x >> y;
      if (a) {
        adj[x].push_back(y);
      } else {
        p.push_back({x, y});
      }
    }
    for (long long int i = 1; i <= n; i++) {
      if (!vis[i]) dfs(i);
      if (flag == 0) break;
    }
    if (!flag)
      cout << "NO"
           << "\n";
    else {
      cout << "YES"
           << "\n";
      for (auto x : p) {
        long long int a = x.first, b = x.second;
        if (tout[a] > tout[b])
          cout << a << " " << b << "\n";
        else
          cout << b << " " << a << "\n";
      }
      for (long long int i = 1; i <= n; i++) {
        for (long long int x : adj[i]) cout << i << " " << x << "\n";
      }
    }
  }
}
