#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;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdmVjdG9yPHBhaXI8aW50LGludD4gPiBhZGpbNTAwMDArNV07CnBhaXI8aW50LGludD4gYmZzKGludCB4LGludCBuKXsKICAgIGJvb2wgdmlzW24rMV07CiAgICBpbnQgZGlzdFtuKzFdOwogICAgZm9yKGludCBpPTA7aTw9bjtpKyspewogICAgICAgIGRpc3RbaV09MDsKICAgICAgICB2aXNbaV09ZmFsc2U7CiAgICB9CiAgICBkaXN0W3hdPTA7CiAgICBxdWV1ZTxpbnQ+cTsKICAgIHEucHVzaCh4KTsKICAgIHZpc1t4XT10cnVlOwogICAgd2hpbGUoIXEuZW1wdHkoKSl7CiAgICAgICAgaW50IHA9cS5mcm9udCgpOwogICAgICAgIHEucG9wKCk7CiAgICAgICAgZm9yKGludCBpPTA7aTxhZGpbcF0uc2l6ZSgpO2krKyl7CiAgICAgICAgICAgIGludCBlPWFkaltwXVtpXS5maXJzdDsKICAgICAgICAgICAgaW50IHc9YWRqW3BdW2ldLnNlY29uZDsKICAgICAgICAgICAgaWYoIXZpc1tlXSl7CiAgICAgICAgICAgICAgICBkaXN0W2VdPWRpc3RbcF0rdzsKICAgICAgICAgICAgICAgIHEucHVzaChlKTsKICAgICAgICAgICAgICAgIHZpc1tlXT10cnVlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaW50IG5vZGUsd2VpZ2h0PTA7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgaWYoZGlzdFtpXT53ZWlnaHQpewogICAgICAgICAgICBub2RlPWk7CiAgICAgICAgICAgIHdlaWdodD1kaXN0W2ldOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBtYWtlX3BhaXIobm9kZSx3ZWlnaHQpOwp9CgppbnQgbG9uZ2VzdFBhdGgoaW50IG4pewogICAgcGFpcjxpbnQsaW50PiB0MSx0MjsKICAgIHQxPWJmcygxLG4pOwogICAgdDI9YmZzKHQxLmZpcnN0LG4pOwogICAgcmV0dXJuIHQyLnNlY29uZDsKfQoKaW50IG1haW4oKXsKICAgIGludCB0OwogICAgY2luPj50OwogICAgd2hpbGUodC0tKXsKICAgICAgICBpbnQgbjsKICAgICAgICBjaW4+Pm47CiAgICAgICAgZm9yKGludCBpPTA7aTw9bjtpKyspCiAgICAgICAgICAgIGFkaltpXS5jbGVhcigpOwogICAgICAgIGludCBhLGIsbDsKICAgICAgICBmb3IoaW50IGk9MDtpPG4tMTtpKyspewogICAgICAgICAgICBjaW4+PmE+PmI+Pmw7CiAgICAgICAgICAgIGFkalthXS5wdXNoX2JhY2soe2IsbH0pOwogICAgICAgICAgICBhZGpbYl0ucHVzaF9iYWNrKHthLGx9KTsKICAgICAgICB9CiAgICAgICAgY291dDw8bG9uZ2VzdFBhdGgobik8PGVuZGw7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==