#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const long long mod = 1e9 + 7;
int T, n, m;
int id[maxn], du[maxn];
vector<int> g[maxn];
signed main() {
  int op, u, v;
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &m);
    vector<pair<int, int>> ans;
    for (int i = 1; i <= n; ++i) g[i].clear(), du[i] = 0, id[i] = 0;
    for (int i = 1; i <= m; ++i) {
      scanf("%d %d %d", &op, &u, &v);
      ans.push_back(make_pair(u, v));
      if (op) g[u].push_back(v), du[v]++;
    }
    queue<int> que;
    int cnt = 0;
    for (int i = 1; i <= n; ++i)
      if (!du[i]) que.push(i);
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      id[u] = ++cnt;
      for (auto v : g[u]) {
        du[v]--;
        if (du[v] == 0) {
          que.push(v);
        }
      }
    }
    if (cnt < n)
      puts("NO");
    else {
      puts("YES");
      for (auto v : ans) {
        if (id[v.first] > id[v.second]) swap(v.first, v.second);
        cout << v.first << ' ' << v.second << '\n';
      }
    }
  }
  return 0;
}
