#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200005];
vector<int> st;
map<int, int> cnt;
vector<bool> vis(200005);
vector<pair<int, int>> edge;
void topSort(int n) {
  vector<int> ans;
  int count = 0;
  while (!st.empty()) {
    auto it = st.begin();
    int now = *it;
    count++;
    st.erase(it);
    ans.push_back(now);
    for (auto v : adj[now]) {
      if (cnt[v]) cnt[v]--;
      if (!cnt[v] && !vis[v]) {
        st.push_back(v);
        vis[v] = 1;
      }
    }
  }
  if (ans.size() != n) {
    cout << "NO"
         << "\n";
    return;
  }
  vector<int> pos(n);
  for (int i = 0; i < n; ++i) {
    pos[ans[i]] = i;
  }
  cout << "YES"
       << "\n";
  for (auto e : edge) {
    if (pos[e.first] < pos[e.second]) {
      cout << e.first + 1 << " " << e.second + 1 << "\n";
    } else {
      cout << e.second + 1 << " " << e.first + 1 << "\n";
    }
  }
}
void clr(int n) {
  for (int i = 0; i < n; ++i) {
    adj[i].clear();
    cnt[i] = 0;
    vis[i] = 0;
  }
  st.clear();
  edge.clear();
}
void solve() {
  int n, m;
  cin >> n >> m;
  clr(n);
  int t, x, z;
  for (int i = 0; i < m; ++i) {
    cin >> t >> x >> z;
    --x, --z;
    if (t) {
      adj[x].push_back(z);
      cnt[z]++;
    }
    edge.push_back({x, z});
  }
  for (int i = 0; i < n; ++i) {
    if (!cnt[i]) st.push_back(i);
  }
  topSort(n);
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
