#include <bits/stdc++.h>

using namespace std;

/*
if(used[to]) low[v]=min(low[v],tin[to])
else{
    dfs(to,v);
    low[v]=min(low[v],low[to]);

    if(low[to]>tin[v]) rebro;
}*/
#define ll long long

vector<pair<ll,ll> > g[1400005];

ll per(ll x,ll y,ll c){

    if(x>y) return 0;
    ll i=0,j=0,mx=0;
    while(true){
            if(i==g[x].size()||j==g[y].size()) break;

        if(g[x][i].first<g[y][j].first) {i++;continue;}

        if(g[x][i].first>g[y][j].first) {j++;continue;}

        mx=max(mx,g[x][i].second+g[y][j].second);
        i++;
    }

    return mx+c;
}

void solve(){

    ll n;
    cin>>n;

    for(ll i=1,x,y,c;i<=n;++i) cin>>x>>y>>c,g[x].push_back({y,c}),g[y].push_back({x,c});

    for(ll i=1;i<=n*2;++i) sort(g[i].begin(),g[i].end());
    ll mx=0;
    for(ll i=1;i<=n*2;++i){
            ll s=0;

        for(auto z:g[i]) mx=max(mx,per(z.first,i,z.second)),s+=z.second;
        mx=max(mx,s);

    }

    cout<<mx<<"\n";

    for(ll i=1;i<=n*2;++i) g[i].clear();
}

int main()
{
    ll q;
    cin>>q;
    while(q--) solve();
}
