#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n;
vector<int> nei[N], dir[N], ord;
int num[N];
bool used[N];
void dfs(int v) {
  used[v] = true;
  for (int to : dir[v])
    if (!used[to]) dfs(to);
  ord.push_back(v);
}
void topsort() {
  ord.clear();
  for (int v = 1; v <= n; ++v) used[v] = false;
  for (int v = 1; v <= n; ++v)
    if (!used[v]) dfs(v);
  reverse(ord.begin(), ord.end());
}
void solve() {
  int m;
  cin >> n >> m;
  for (int v = 1; v <= n; ++v) {
    nei[v].clear();
    dir[v].clear();
  }
  while (m--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0) {
      nei[u].push_back(v);
      nei[v].push_back(u);
    } else
      dir[u].push_back(v);
  }
  topsort();
  for (int i = 0; i < n; ++i) num[ord[i]] = i;
  bool poss = true;
  vector<pair<int, int>> ans;
  for (int v = 1; v <= n && poss; ++v)
    for (int to : dir[v])
      if (num[v] > num[to]) {
        poss = false;
        break;
      } else
        ans.emplace_back(v, to);
  if (!poss) {
    cout << "NO\n";
    return;
  }
  for (int v = 1; v <= n; ++v)
    for (int to : nei[v])
      if (num[v] <= num[to]) ans.emplace_back(v, to);
  cout << "YES\n";
  for (auto& p : ans) cout << p.first << ' ' << p.second << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
