#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> > adj[50000+5];
pair<int,int> bfs(int x,int n){
    bool vis[n+1];
    int dist[n+1];
    for(int i=0;i<=n;i++){
        dist[i]=0;
        vis[i]=false;
    }
    dist[x]=0;
    queue<int>q;
    q.push(x);
    vis[x]=true;
    while(!q.empty()){
        int p=q.front();
        q.pop();
        for(int i=0;i<adj[p].size();i++){
            int e=adj[p][i].first;
            int w=adj[p][i].second;
            if(!vis[e]){
                dist[e]=dist[p]+w;
                q.push(e);
                vis[e]=true;
            }
        }
    }
    int node,weight=0;
    for(int i=1;i<=n;i++){
        if(dist[i]>weight){
            node=i;
            weight=dist[i];
        }
    }
    return make_pair(node,weight);
}

int longestPath(int n){
    pair<int,int> t1,t2;
    t1=bfs(1,n);
    t2=bfs(t1.first,n);
    return t2.second;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=0;i<=n;i++)
            adj[i].clear();
        int a,b,l;
        for(int i=0;i<n-1;i++){
            cin>>a>>b>>l;
            adj[a].push_back({b,l});
            adj[b].push_back({a,l});
        }
        cout<<longestPath(n)<<endl;
    }
    return 0;
}