#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e4 + 5, M = 5e5 + 5, inf = (1 << 31) - 1;
ll n, m, s, ans[N], t;
struct node
{
ll x, y, k;
void clear()
{
x = y = k = 0;
return;
}
void read()
{
cin >> x >> y >> k;
return;
}
} e[M];
void SPFA()
{
for (int i = 1; i < M; i++)
e[i].clear();
cin >> n >> m;
ll p = m;
for (int i = 1; i <= m; i++)
{
e[i].read();
if (e[i].k >= 0)
e[++p] = {e[i].y, e[i].x, e[i].k};
}
for (int i = 1; i <= n; i++)
ans[i] = inf;
ans[1] = 0;
for (int i = 1; ; i++)
{
bool flag = false;
for (int j = 1; j <= m; j++)
if (ans[e[j].y] > ans[e[j].x] + e[j].k)
{
ans[e[j].y] = ans[e[j].x] + e[j].k;
flag = true;
}
if (!flag and i <= n)
break;
if (flag and i > n)
{
cout << "NO\n";
return;
}
}
cout << "YES\n";
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--)
SPFA();
return 0;
}
qwq