#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 6;
int conn[maxn];
int t, n, m, len1, len2;
vector<int> ve[maxn];
int dfn[maxn];
int du[maxn];
void init() {
  for (int i = 0; i <= 2 * n; i++) {
    ve[i].clear();
    dfn[i] = 0;
    du[i] = 0;
  }
  len1 = len2 = 0;
}
void solve() {
  queue<int> que;
  for (int i = 1; i <= n; i++) {
    if (!du[i]) {
      que.push(i);
      dfn[i] = ++len2;
    }
  }
  while (!que.empty()) {
    int top = que.front();
    que.pop();
    for (int i = 0; i < ve[top].size(); i++) {
      du[ve[top][i]]--;
      if (!du[ve[top][i]]) {
        dfn[ve[top][i]] = ++len2;
        que.push(ve[top][i]);
      }
    }
  }
  if (len2 == n) {
    cout << "yes" << endl;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < ve[i].size(); j++) {
        cout << i << " " << ve[i][j] << endl;
      }
    }
    for (int i = 1; i <= len1; i += 2) {
      int pre = conn[i];
      int last = conn[i + 1];
      if (dfn[pre] < dfn[last])
        cout << pre << " " << last << endl;
      else
        cout << last << " " << pre << endl;
    }
  } else
    cout << "no" << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    init();
    for (int i = 1, op, u, v; i <= m; i++) {
      cin >> op >> u >> v;
      if (op == 0)
        conn[++len1] = u, conn[++len1] = v;
      else {
        ve[u].push_back(v);
        du[v]++;
      }
    }
    solve();
  }
  return 0;
}
