#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> connected;
vector<long long> topological_sort() {
  vector<long long> res;
  long long N = connected.size();
  vector<long long> cnt(N, 0);
  for (long long i = 0; i < N; ++i) {
    for (long long j = 0; j < connected.at(i).size(); ++j) {
      cnt.at(connected.at(i).at(j)) += 1;
    }
  }
  queue<long long> q;
  for (long long i = 0; i < N; ++i) {
    if (cnt.at(i) == 0) {
      q.push(i);
      res.push_back(i);
    }
  }
  while (!q.empty()) {
    long long now = q.front();
    q.pop();
    for (long long i = 0; i < connected.at(now).size(); ++i) {
      long long next = connected.at(now).at(i);
      if (cnt.at(next) == 0) {
        continue;
      }
      cnt.at(next) -= 1;
      if (cnt.at(next) == 0) {
        q.push(next);
        res.push_back(next);
      }
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long T;
  cin >> T;
  for (long long testcase = 0; testcase < T; ++testcase) {
    long long N, M;
    cin >> N >> M;
    connected = vector<vector<long long>>(N);
    vector<vector<long long>> input;
    for (long long i = 0; i < M; ++i) {
      long long t, x, y;
      cin >> t >> x >> y;
      x -= 1;
      y -= 1;
      input.push_back({t, x, y});
      if (t == 1) {
        connected.at(x).push_back(y);
      }
    }
    vector<long long> tp_sort = topological_sort();
    if (tp_sort.size() != N) {
      cout << "NO"
           << "\n";
      continue;
    } else {
      cout << "YES"
           << "\n";
    }
    vector<long long> place(N);
    for (long long i = 0; i < N; ++i) {
      place.at(tp_sort.at(i)) = i;
    }
    for (long long i = 0; i < M; ++i) {
      long long t = input.at(i).at(0), x = input.at(i).at(1),
                y = input.at(i).at(2);
      if (t == 1) {
        cout << x + 1 << ' ' << y + 1 << "\n";
      } else {
        if (place.at(x) < place.at(y)) {
          cout << x + 1 << ' ' << y + 1 << "\n";
        } else {
          cout << y + 1 << ' ' << x + 1 << "\n";
        }
      }
    }
  }
}
