#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2*1e4;
int w[maxn];
int to[maxn],first[maxn],nxt[maxn],num=1;
int dis[maxn],inq[maxn],q[maxn],cnt[maxn];
int head,tail;
void add(int a,int b,int c){
to[num]=b;
w[num]=c;
nxt[num]=first[a];
first[a]=num;
++num;
}
int n,m,s;
bool spfa(){
memset(dis,0x3f,sizeof dis);
memset(inq,0,sizeof inq);
memset(cnt,0,sizeof cnt);
dis[s]=0,inq[s]=1,q[tail++]=s;
while(head!=tail) {
int tmp=q[head++];
head=head%n;
for(int i=first[tmp];i;i=nxt[i]) {
if(dis[to[i]]>dis[tmp]+w[i]) {
dis[to[i]]=dis[tmp]+w[i];
cnt[to[i]]=cnt[tmp]+1;
if(cnt[to[i]]>=n) return 1;
if(!inq[to[i]]) {
inq[to[i]]=1;
q[tail++]=to[i];
tail=tail%n;
}
}
}
inq[tmp]=0;
}return 0;
}
signed main(){
int T;
cin>>T;
while(T--){
num=1;
memset(nxt,0,sizeof nxt);
memset(first,0,sizeof first);
memset(w,0,sizeof w);
cin>>n>>m;
for(int i=1;i<=m;i++) {
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
if(c>=0) add(b,a,c);
}
s=1;
bool f=spfa();
cout<<(f?"YES\n":"NO\n");
}
return 0;
}
60pts,#1 #2 TLE,#3 #6 WA