#include <iostream>
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
#define endl "\n"
//#define vector<ll> vll;
//#define fi first;
//#define se second;
//#define pb push_back;
typedef pair<ll, ll> pll;
const ll dim=7*(1e5+6);
ll n, t;
string s;
vector<pll> a[dim];
ll path[dim], vis[dim];
deque<ll> dq;
ll ans=0;
ll cur=0;
void dfs(ll v, ll p, ll gp, ll len){
    vis[v]=1;
    while(dq.size()>=3){
        cur-=dq.back();
        dq.pop_back();
    }
    dq.push_front(len);
    cur+=len;
    ans=max(ans, cur);

    for(auto x: a[v]){
        ll to=x.first;

        if(to==gp){
            dfs(x.first, v, p, x.second);

        }
        if(vis[x.first]==1)continue;

    }
    dq.pop_front();


}

int main()
{
    ll x, y, cnt;
    srand(time(0));
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        map<ll, ll> mp;
        for(int i=1; i<=n; i++){
            cin>>x>>y>>cnt;
            a[x].push_back({y, cnt});
            a[y].push_back({x, cnt});
            mp[x]+=cnt;
            mp[y]+=cnt;

        }
        ll mx=0;
        dfs(1, 0, 0, 0);
        for(auto x:mp){
           // cout<<x.first<<" "<<x.second<<endl;
            if(mx<x.second){
                mx=x.second;
            }
        }

        cout<<max(mx, ans)<<endl;
    }

    return 0;
}
