#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int >g[200005];
int du[200005];
int td[200005];
pair<int ,int >a[200005];
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
g[i].clear();
du[i]=0;
td[i]=0;
}
for(int i=1;i<=m;i++){
int q,x,y;
cin>>q>>x>>y;
a[i]=make_pair(x,y);
if(q){
g[x].push_back(y);
du[y]++;
}
}
queue<int >q;
ll tot=0;
for(int i=1;i<=n;i++){
if(!du[i]){
q.push(i);
}
}
while(!q.empty()){
int temp=q.front();
q.pop();
td[temp]=++tot;
for(auto i:g[temp]){
du[i]--;
if(!du[i]){
q.push(i);
}
}
}
if(tot<n){
cout<<"NO"<<endl;
}
else{
cout<<"YES"<<endl;
for(int i=1;i<=m;i++){
int u=a[i].first;
int v=a[i].second;
if(td[u]<td[v])cout<<u<<" "<<v<<endl;
else{
cout<<v<<" "<<u<<endl;
}
}
}
}
int main(){
int t;
cin>>t;
while(t--){
solve();
}
}
https://codeforces.ml/contest/1385/standings/friends/true