#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int s=0,f=1;
char ch=getchar();
while(ch<'0'||'9'<ch) {if(ch=='-') f=-1;ch=getchar();}
while('0'<=ch&&ch<='9') {s=s*10+ch-'0';ch=getchar();}
return s*f;
}
int T,n,m;
vector<int>head,to,nxt,val;
void join(int u,int v,int w)
{
nxt.push_back(head[u]);
head[u]=to.size();
to.push_back(v);
val.push_back(w);
}
#define N 2010
#define M 3010
int dis[N],cnt[N];
bool vis[N];
int q[4*N*N],l=0,r=0;
bool spfa()
{
l=r=0;
for(int i=1;i<=n;++i)
{
dis[i]=0x3f3f3f3f;
cnt[i]=0;
vis[i]=0;
}
vis[1]=1;
q[l]=1;
dis[1]=0;
cnt[1]=1;
while(l<=r)
{
int u=q[l];++l;vis[u]=0;
for(int i=head[u];i!=-1;i=nxt[i])
{
int v=to[i];
//printf("%d %d %d\n",v,dis[v],dis[u]+val[i]);
if(dis[v]>dis[u]+val[i])
{
dis[v]=dis[u]+val[i];
if(!vis[v])
{
++cnt[v];
if(cnt[v]>n) return 0;
vis[v]=1;
q[++r]=v;
}
}
}
}
return 1;
}
int main()
{
T=read();
while(T--)
{
n=read();m=read();
vector<int>().swap(val);
vector<int>().swap(nxt);
vector<int>().swap(to);
vector<int>().swap(head);
head.resize(max(2*n+2,2*m+2),-1);
for(int i=1;i<=m;++i)
{
int u,v,w;
u=read();v=read();w=read();
//printf("cs:%d %d %d\n",u,v,w);
if(w>=0)
{
join(u,v,w);
join(v,u,w);
}
else
{
join(u,v,w);
}
}
if(spfa()) printf("NO\n");
else printf("YES\n");
}
return 0;
}