#include <bits/stdc++.h>
using namespace std;
int in(char &c) { return scanf("%c", &c); }
int in(char s[]) { return scanf("%s", s); }
int in(int &x) { return scanf("%d", &x); }
int in(long long &x) { return scanf("%lld", &x); }
int in(double &x) { return scanf("%lf", &x); }
int in(pair<int, int> &p) { return scanf("%d%d", &p.first, &p.second); }
int in(pair<long long, long long> &p) {
  return scanf("%lld%lld", &p.first, &p.second);
}
int in(pair<double, double> &p) { return scanf("%lf%lf", &p.first, &p.second); }
int in(long double &a) { return scanf("%Lf", &a); }
int in(int &x, long long &y) { return scanf("%d%lld", &x, &y); }
int in(long long &x, int &y) { return scanf("%lld%d", &x, &y); }
int in(int &x, int &y) { return scanf("%d%d", &x, &y); }
int in(long long &x, long long &y) { return scanf("%lld%lld", &x, &y); }
int in(char s[], int &n) { return scanf("%s%d", s, &n); }
int in(int &n, char s[]) { return scanf("%d%s", &n, s); }
int in(char a[], char b[]) { return scanf("%s%s", a, b); }
int in(double &x, double &y) { return scanf("%lf%lf", &x, &y); }
int in(int &n, char s[], char t[]) { return scanf("%d%s%s", &n, s, t); }
int in(int &x, int &y, int &z) { return scanf("%d%d%d", &x, &y, &z); }
int in(long long &x, long long &y, long long &z) {
  return scanf("%lld%lld%lld", &x, &y, &z);
}
int in(double &x, double &y, double &z) {
  return scanf("%lf%lf%lf", &x, &y, &z);
}
int in(int &x, int &y, int &z, int &k) {
  return scanf("%d%d%d%d", &x, &y, &z, &k);
}
int in(long long &x, long long &y, long long &z, long long &k) {
  return scanf("%lld%lld%lld%lld", &x, &y, &z, &k);
}
int in(double &x, double &y, double &z, double &k) {
  return scanf("%lf%lf%lf%lf", &x, &y, &z, &k);
}
const int N = 3e5 + 10, M = 1e3 + 10, mod = 1e9 + 7;
const double eps = 1e-6, Pi = acos(-1.0);
vector<int> g[N];
int dep[N], vis[N], op[N], u[N], v[N], In[N], col[N], use[N], Out[N];
queue<int> que;
int topsort(int n) {
  for (int i = 1; i <= n; i++) {
    if (In[i] == 0) {
      que.push(i);
    }
  }
  int cnt = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    dep[u] = ++cnt;
    for (auto e : g[u]) {
      In[e]--;
      if (In[e] == 0) {
        que.push(e);
      }
    }
  }
  return (n != cnt);
}
int main() {
  int T, c = 0, t;
  in(T);
  t = T;
  while (T--) {
    c++;
    int n, m;
    in(n, m);
    for (int i = 1; i <= n; i++) In[i] = dep[i] = vis[i] = 0, g[i].clear();
    for (int i = 1; i <= m; i++) {
      in(op[i], u[i], v[i]);
      if (op[i] == 1) {
        In[v[i]]++;
        g[u[i]].push_back(v[i]);
      }
    }
    if (topsort(n)) {
      printf("NO\n");
      continue;
    }
    printf("YES\n");
    for (int i = 1; i <= m; i++) {
      if (op[i] == 0) {
        if (dep[u[i]] > dep[v[i]]) swap(u[i], v[i]);
      }
      printf("%d %d\n", u[i], v[i]);
    }
  }
  return 0;
}
