rt
dalao 救救孩子吧,十分感谢
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
inline int read(){
int x = 0,f = 1;
char ch = getchar();
while(!isdigit(ch)){
if(ch == '-'){
f = -1;
}
ch = getchar();
}
while(isdigit(ch)){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
vector <int> tt[N];
int ord[N],cnt,st[N],ed[N],wx;
int n,m,T,v[N],vv[N],f,rd[N],cd[N];
void dfs(int x){
v[x] = 1;
vv[x] = 1;
for(int i = 0;i < tt[x].size();i++){
int y = tt[x][i];
if(v[y]){
f = 1;
}else{
dfs(y);
}
}
v[x] = 0;
}
void topo(){
queue <int> q;
for(int i = 1;i <= n;i++){
ord[i] = 2147483647;
if(rd[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int x = q.front();
q.pop();
ord[x] = ++cnt;
for(int i = 0 ;i < tt[x].size();i++){
int y = tt[x][i];
rd[y]--;
if(rd[y] == 0){
q.push(y);
}
}
}
}
int main(){
// freopen("data.in","r",stdin);
// freopen("A.out","w",stdout);
T = read();
while(T--){
n = read();m = read();
cnt = wx = 0;
for(int i = 1;i <= n;i++){
v[i] = vv[i] = rd[i] = cd[i] = 0;
tt[i].clear();
}
for(int i = 1;i <= m;i++){
int ty = read(),x = read(),y = read();
if(ty == 0){
st[++wx] = x;
ed[wx] = y;
}else{
tt[x].push_back(y);
rd[y]++,cd[x]++;
}
}
f = 0;
for(int i = 1;i <= n;i++){
if(!vv[N])dfs(i);
}
if(f == 1){
cout << "NO\n";
continue;
}
cout << "YES\n";
topo();
// cout <<"ORD:\n";
// for(int i = 1;i <= n;i++){
// cout << ord[i] << " " ;
// }
// cout << endl;
for(int x = 1;x <= n;x++){
for(int i = 0;i < tt[x].size();i++){
int y = tt[x][i];
printf("%d %d\n",x,y);
}
}
for(int i = 1;i <= wx;i++){
if(ord[st[i]] > ord[ed[i]])swap(st[i],ed[i]);
printf("%d %d\n",st[i],ed[i]);
}
}
return 0;
}