#include <bits/stdc++.h>
using namespace std;
template <class T = long long>
using V = vector<T>;
template <class T = long long>
using VV = V<V<T>>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\n';
    exit(0);
  }
}
template <class T>
inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\n';
    exit(0);
  }
}
template <class T>
T Roundup_div(T x, T y) {
  return (x + (y - 1)) / y;
}
const long long INF = 1e18;
template <typename T>
T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1) ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
struct Edge {
  int to, dir;
  Edge(int to, int dir) : to(to), dir(dir) {}
};
using Graph = VV<Edge>;
long long n, m;
void solve() {
  cin >> n >> m;
  Graph G(n);
  V<> h(n, 0);
  V<pair<int, int>> res;
  V<pair<int, int>> kep;
  for (long long i = (0); i < (m); ++i) {
    int u, v, t;
    cin >> t >> u >> v;
    u--;
    v--;
    if (t) {
      G[u].push_back({v, t});
      h[v]++;
      res.push_back({u, v});
    } else
      kep.push_back({u, v});
  }
  V<> ans;
  stack<int> st;
  for (long long i = (0); i < (n); ++i)
    if (h[i] == 0) st.push(i);
  while (st.size()) {
    int i = st.top();
    st.pop();
    ans.push_back(i);
    for (auto& j : G[i]) {
      h[j.to]--;
      if (h[j.to] == 0) st.push(j.to);
    }
  }
  if (((long long)(ans).size()) != n) {
    cout << "NO" << endl;
    return;
  }
  cout << "YES" << endl;
  V<> place(n);
  for (long long i = (0); i < (n); ++i) place[ans[i]] = i;
  for (auto v : res) cout << v.first + 1 << " " << v.second + 1 << endl;
  for (auto v : kep) {
    if (place[v.second] - place[v.first] < 0)
      cout << v.second + 1 << " " << v.first + 1 << endl;
    else
      cout << v.first + 1 << " " << v.second + 1 << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
}
