#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> dir(N + 1);
    vector<pair<int, int>> indir;
    vector<int> isin(N + 1);
    for (int m = 0; m < M; ++m) {
      int T, X, Y;
      cin >> T >> X >> Y;
      if (T) {
        dir[X].push_back(Y);
        isin[Y]++;
      } else
        indir.push_back({X, Y});
    }
    queue<int> q, ans;
    for (int n = 1; n <= N; ++n) {
      if (isin[n] == 0) q.push(n);
    }
    while (!q.empty()) {
      int tmp = q.front();
      ans.push(tmp);
      q.pop();
      for (auto x : dir[tmp]) {
        isin[x]--;
        if (isin[x] == 0) q.push(x);
      }
    }
    if (ans.size() == N) {
      vector<int> lev(N + 1);
      int lv = 1;
      while (!ans.empty()) {
        int tmp = ans.front();
        ans.pop();
        lev[tmp] = lv++;
      }
      for (auto x : indir) {
        int a = x.first, b = x.second;
        if (lev[a] < lev[b])
          dir[a].push_back(b);
        else
          dir[b].push_back(a);
      }
      cout << "YES\n";
      for (int i = 1; i <= N; ++i) {
        for (auto x : dir[i]) {
          cout << i << ' ' << x << '\n';
        }
      }
    } else
      cout << "NO\n";
  }
  return 0;
}
