#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int h[N], w[N], e[N], ne[N], idx;
int dist[N], cnt[N];
bool st[N];
int n, m;
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
bool spfa()
{
queue<int> q;
q.push(1);
st[1] = true;
while(q.size())
{
auto t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n)
return true;
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T --)
{
memset(h, -1, sizeof h);
memset(dist, 0, sizeof dist);
memset(st, false, sizeof st);
cin >> n >> m;
for (int i = 1; i <= m; i ++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
if(spfa())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}