#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 10000000;


int deikstra(int s, int n, vector<vector< pair<int, int> > > &g) {
	vector<int> d (n, INF);
	d[s] = 0;
	priority_queue < pair< int, int > > q;
	q.push (make_pair (0, s));
	while (!q.empty()) {
		int v = q.top().second, 
			cur_d = -q.top().first;
		q.pop();
		if (cur_d > d[v]) continue;
		for (int j = 0; j < g[v].size(); ++j) {
			int to = g[v][j].first,
				len = g[v][j].second;
			
			if (d[v] + len < d[to]) {
				d[to] = d[v] + len;
				q.push(make_pair(-d[to], to));
			}
		}
	}
	int max = -1;
	for (int i =0; i <n; ++i) {
		if ( d[i] != INF && i != s && max < d[i]) {
			max = d[i];
			
		}
	}
	return max;
}




int main()
{
	freopen("SOCHI.IN", "r", stdin);
	int t;
	cin >> t;
	while (t--)
	{	
		int n ,m;
		cin >> n >> m;
		vector<vector< pair<int, int> > > g(n);
		for (int i = 0; i < m; i++) {
			int a,b,cost;
			cin >>a >>b>>cost;
			a--;
			b--;
			g[a].push_back(make_pair(b, cost));
		}
		int max = -1;
		for (int i =0 ; i <n; ++i) {
			int dmax = deikstra(0, n, g);
			if (dmax > max)
				max = dmax;

		}
		cout << max <<endl;
	}

	return 0;
}
