#include <bits/stdc++.h>
using namespace std;
 
vector<pair<int , int> > G[50005];
 
int bfs1(int x){
	queue<pair<int , long long> > q;
	bool visited[50005];
	fill(visited,visited+50000,false);
	q.push(make_pair(x , 0));
	long long d=0 ; int node = x;
	pair<int , long long> temp;
	while(!q.empty()){
		 temp = q.front(); q.pop();
		 if(temp.second >= d) { d= temp.second ; node = temp.first; }
		 visited[temp.first]=1;
		for(auto i=G[temp.first].begin() ; i!=G[temp.first].end() ; i++){
			if(!visited[i->first]) q.push(make_pair(i->first , temp.second+i->second));
		}
	}
	return node;
}
 
long long bfs2(int x){
	queue<pair<int , long long> > q;
	bool visited[50005];
	fill(visited,visited+50000,false);
	q.push(make_pair(x , 0));
	long long d=0 ; int node = x;
	pair<int , long long> temp;
	while(!q.empty()){
		 temp = q.front(); q.pop();
		 if(temp.second >= d) { d= temp.second ; node = temp.first; }
		 visited[temp.first]=1;
		for(auto i=G[temp.first].begin() ; i!=G[temp.first].end() ; i++){
			if(!visited[i->first]) q.push(make_pair(i->first , temp.second+i->second));
		}
	}
	return d;
}
 
int main() {
	ios_base::sync_with_stdio(false);
	int t , n , a , b , c , x;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0 ; i<=n ; i++) G[i].clear();
		for(int i=1 ; i<n ; i++){
			cin>>a>>b>>c;
			G[a].push_back(make_pair(b , c));
			G[b].push_back(make_pair(a , c));
		}
 
		x = bfs1(2);
		cout<<bfs2(x)<<"\n";
	}
	return 0;
}