#include <bits/stdc++.h>
using namespace std;
int in[200005], id[200005];
vector<int> G[200005];
vector<pair<int, int>> undir, ret;
int main() {
  ios::sync_with_stdio(0);
  int T, n, m;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
      G[i].clear();
      in[i] = id[i] = 0;
    }
    undir.clear();
    ret.clear();
    for (int i = 1, op, u, v; i <= m; ++i) {
      cin >> op >> u >> v;
      if (op == 1) {
        in[v]++;
        G[u].push_back(v);
        ret.push_back({u, v});
      } else {
        if (u > v) swap(u, v);
        undir.push_back({u, v});
      }
    }
    sort(undir.begin(), undir.end());
    priority_queue<int, vector<int>, greater<int>> q;
    for (int i = 1; i <= n; i++)
      if (in[i] == 0) q.push(i);
    vector<int> ans;
    while (!q.empty()) {
      int p = q.top();
      q.pop();
      ans.push_back(p);
      for (auto to : G[p]) {
        in[to]--;
        if (in[to] == 0) q.push(to);
      }
    }
    if (ans.size() < n) {
      cout << "NO\n";
      continue;
    }
    for (int i = 0; i < n; ++i) id[ans[i]] = i + 1;
    bool ok = 1;
    for (auto i : undir) {
      if (id[i.first] < id[i.second])
        ret.push_back(i);
      else
        ret.push_back({i.second, i.first});
    }
    cout << "YES\n";
    for (auto i : ret) cout << i.first << " " << i.second << "\n";
  }
  return 0;
}
