#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int inf = 2e9 + 5;
double PI = 3.14159265358979323846;
const int N = 2e5 + 5;
int n, m;
int currentOrder;
vector<int> g[N];
vector<int> order(N);
vector<int> visited(N);
bool cycle = 0;
void dfs(int u) {
  visited[u] = 1;
  for (auto v : g[u]) {
    if (visited[v] == 1) {
      cycle = 1;
    } else if (!visited[v]) {
      dfs(v);
    }
  }
  visited[u] = 2;
  order[u] = currentOrder++;
}
void solve() {
  cin >> n >> m;
  vector<pair<int, int> > edges;
  for (int i = 1; i <= n; i++) {
    g[i].clear(), visited[i] = 0;
    order[i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    int op, u, v;
    cin >> op >> u >> v;
    if (op) g[u].push_back(v);
    edges.push_back({u, v});
  }
  currentOrder = 1;
  cycle = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
    }
  }
  if (cycle) {
    cout << "NO"
         << "\n";
    return;
  }
  cout << "YES"
       << "\n";
  for (auto &[u, v] : edges) {
    if (order[u] < order[v]) {
      swap(u, v);
    }
    cout << u << " " << v << "\n";
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
