#include<iostream>#include<queue>#include<vector>#include<algorithm>usingnamespace std;typedef pair<int,int> pll;constint maxn =2e5+5;
vector<int> g[maxn];
vector<pll> edges;int ind[maxn];int pos[maxn];boolKahnTopological(int n){queue<int> q;for(int i =0; i < n;++i){if(ind[i]==0){q.push(i);}}int cnt =0;while(!q.empty()){int u = q.front(); q.pop();pos[u]= cnt++;for(int i =0; i < g[u].size(); i++){int v = g[u][i];--ind[v];if(ind[v]==0){q.push(v);}}}if(cnt == n){puts("YES");for(int i =0; i < edges.size();++i){int u = edges[i].first;int v = edges[i].second;if(pos[u]> pos[v])swap(u,v);printf("%d %d\n",u+1,v+1);}}else{puts("No");}}voidinit(int n){for(int i =0; i < n;++i){g[i].clear();ind[i]=0;pos[i]=0;}edges.clear();}voidsolve(){int n, m, t, x, y;scanf("%d%d",&n,&m);init(n);for(int i =0; i < m;++i){scanf("%d%d%d",&t,&x,&y);--x,--y; edges.push_back(pll(x, y));if(t ==1){g[x].push_back(y);++ind[y];}}KahnTopological(n);}intmain(){int t;scanf("%d",&t);for(int i =0; i < t;++i)solve();return0;}