#include <bits/stdc++.h>
#define buff \
ios::sync_with_stdio(false); \
cin.tie(0);
//#define int long long
using namespace std;
const int N = 2e5 + 9;
int n, m;
int e[N], ne[N], h[N], idx;
int d[N], q[N], s[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
struct Edge
{
int x, y;
} edge[N];
int cnt;
bool topsort()
{
int tt = -1, hh = 0;
for (int i = 1; i <= n; i++)
if (!d[i])
q[++tt] = i;
while (hh <= tt)
{
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
d[j]--;
if (!d[j])
q[++tt] = j;
}
}
return tt == n - 1;
}
void solve()
{
cin >> n >> m;
memset(h, -1, (n + 1) * 4);
memset(d, 0, (n + 1) * 4);
idx = cnt = 0;
for (int i = 1; i <= m; i++)
{
int t, a, b;
cin >> t >> a >> b;
if (t == 0)
edge[cnt++] = {a, b};
else
add(a, b), d[b]++;
}
if (!topsort())
{
cout << "NO\n";
return;
}
cout << "YES\n";
for (int i = 1; i <= n; i++)
{
for (int j = h[i]; ~j; j = ne[j])
{
cout << i << ' ' << e[j] << '\n';
}
}
for (int i = 0; i < n; i++)
s[q[i]] = i;
for (int i = 0; i < cnt; i++)
{
int a = edge[i].x, b = edge[i].y;
if (s[a] > s[b])
swap(a, b);
cout << a << ' ' << b << '\n';
}
}
int main()
{
int T;
cin >> T;
while (T--)
solve();
}