#include <bits/stdc++.h>
using namespace std;
struct ll
{
int x,w;
};
int main()
{
int t;
cin >> t;
while (t--)
{
vector <ll> v[5010];
int num[5010] = {},dis[5010] = {},n,m;
bool f[5010] = {},flag = true;
cin >> n >> m;
for(int i = 1 ; i <= m ; i++)
{
int x,y,w;
cin >> x >> y >> w;
v[x - 1].push_back({y , w});
v[y].push_back({x - 1 , -w});
}
queue <int> q;
for(int i = 1 ; i <= n ; i++)
{
dis[i] = 0;
num[i] = 1;
q.push(i);
}
while(!q.empty())
{
int tmp = q.front();
q.pop();
if(num[tmp] > n)
{
cout << "false" << endl;
flag = false;
break;
}
for(auto i : v[tmp])
{
if(dis[i.x] > dis[tmp] + i.w)
{
dis[i.x] = dis[tmp] + i.w;
num[i.x] = num[tmp] + 1;
q.push(i.x);
}
}
}
if(flag) cout << "true" << endl;
}
return 0;
}