#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const long long mod = 998244353;
vector<int> g[maxn];
int d[maxn], dis[maxn];
struct edge {
  int u, v;
} e[maxn], e1[maxn];
queue<int> q;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);
    for (int i = (1); i <= (n); i++) g[i].clear(), d[i] = 0;
    int cnt = 0, cnt1 = 0;
    for (int i = (1); i <= (m); i++) {
      int t;
      scanf("%d", &t);
      int x;
      scanf("%d", &x);
      int y;
      scanf("%d", &y);
      if (t == 0)
        e[++cnt] = (edge){x, y};
      else {
        g[x].push_back(y), d[y]++;
        e1[++cnt1] = (edge){x, y};
      }
    }
    for (int i = (1); i <= (n); i++)
      if (!d[i]) q.push(i);
    int cnt2 = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      dis[u] = ++cnt2;
      for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        d[v]--;
        if (!d[v]) q.push(v);
      }
    }
    if (cnt2 < n)
      puts("NO");
    else {
      puts("YES");
      for (int i = (1); i <= (cnt1); i++)
        printf("%d ", e1[i].u), printf("%d ", e1[i].v), puts("");
      for (int i = (1); i <= (cnt); i++) {
        if (dis[e[i].u] > dis[e[i].v]) swap(e[i].u, e[i].v);
        printf("%d ", e[i].u), printf("%d ", e[i].v), puts("");
      }
    }
  }
}
