#include <bits/stdc++.h>
using namespace std;
inline bool kthbit(long long n, int k) { return (n >> k) & 1; }
long long pw(long long a, long long b) {
  long long c = 1, m = a;
  while (b) {
    if (b & 1) c = (c * m);
    m = (m * m);
    b /= 2;
  }
  return c;
}
vector<vector<long long> > adj;
vector<bool> visited;
vector<long long> depurture;
void dfs(long long v) {
  visited[v] = true;
  for (auto to : adj[v]) {
    if (!visited[to]) dfs(to);
  }
  depurture.push_back(v);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long T = 1;
  cin >> T;
  while (T--) {
    vector<pair<pair<long long, long long>, long long> > edges;
    depurture.clear();
    long long n, m, x, y, t;
    cin >> n >> m;
    adj = vector<vector<long long> >(n);
    visited = vector<bool>(n, false);
    for (long long i = 0; i < m; ++i) {
      cin >> t >> x >> y;
      x--;
      y--;
      if (t == 1) {
        adj[x].push_back(y);
        edges.push_back({{x, y}, 1});
      } else {
        edges.push_back({{x, y}, 0});
      }
    }
    for (long long i = 0; i < n; ++i) {
      if (!visited[i]) dfs(i);
    }
    reverse((depurture).begin(), (depurture).end());
    unordered_map<long long, long long> pos;
    for (long long i = 0; i < n; ++i) {
      pos[depurture[i]] = i;
    }
    bool ok = true;
    for (long long v = 0; v < n; ++v) {
      for (auto to : adj[v]) {
        if (pos[v] > pos[to]) {
          ok = false;
        }
      }
    }
    if (ok) {
      cout << "YES"
           << "\n";
      for (auto i : edges) {
        if (i.second == 0) {
          if (pos[i.first.first] < pos[i.first.second])
            cout << i.first.first + 1 << " " << i.first.second + 1 << "\n";
          else
            cout << i.first.second + 1 << " " << i.first.first + 1 << "\n";
        } else {
          cout << i.first.first + 1 << " " << i.first.second + 1 << "\n";
        }
      }
    } else {
      cout << "NO"
           << "\n";
    }
  }
  return 0;
}
