#include <bits/stdc++.h>
#include <random>

using namespace std;

#define ll long long
#define endl '\n'
#define ld long double
#define pb push_back

const ll clasik = 7e5+7;
const ll INF = 1e17;
const ll mod = 1e9+7;
const ll mln = 1e6+111;

ll pref[2*mln];
ll df[mln];

vector <pair<ll,ll> > g[2*clasik];

ll pos[2*clasik];

ll cntv[2*clasik];

ll used[2*clasik];
ll res = 0;

deque <pair<ll,ll> > q;
ll zar[2*clasik] , pot[2*clasik];
void bfs(int v) {
    q.pb({v , 0});
    zar[v] = 0;
    pot[v] = 0;
    while (q.size()) {
        pair<ll,ll> pr = q.front();
        q.pop_front();
        ll u = pr.first;
        zar[u] = pr.second;
        for (auto to : g[u]) {
            if (pot[to.first] == 0) {
                q.push_back({to.first , to.second});
                pot[to.first] = pot[u]+1;
                continue;
            }
            if (pot[to.first] == pot[u]) {
                
                res = max(res , to.second + zar[to.first] + zar[u]);
                //if (res==12) cout<<to.first<<" "<<u<<endl;
            }
        }
    }

}

void solve() {

    ll n;

    cin>>n;
    ll a , b , c;
    for (int i = 1 ; i<=n ; i++) {
        cin>>a>>b>>c;
        g[a].pb({b , c});
        g[b].pb({a , c});
        cntv[a]+=c;
        cntv[b]+=c;
    }
    res = 0;
    for (int i = 1 ; i<=2*n ; i++) {
        res = max(res , cntv[i]);      
    }

    for (int i =1 ;i<=2*n ; i++) {
        if (pot[i] ==0) {
            bfs(i);
        }
    }

    cout<<res<<endl;

    // tut chistim

    for (int i = 1 ; i<=2*n ; i++) {
        used[i] = 0;
        cntv[i] = 0;
        g[i].clear();
        pot[i] = 0;
        zar[i] = 0;
    }

}

signed main() {
    
    int t = 1;

    cin>>t;

    while(t--) solve();

    return 0;
}