#include<bits/stdc++.h>
/*#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")*/
#define ll int
#define fr first
#define sc second
#define pb push_back
using namespace std;
ll mp1[100005];
map<pair<ll, ll>, ll>mp;
vector<vector<pair<ll, ll>>>g1(100005);
vector<pair<ll, pair<ll, ll>>>g;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        g.clear();
        for(ll i=0;i<n;i++){
            ll a, b, c;
            cin>>a>>b>>c;
            g.pb({c, {a, b}});
            mp1[a]+=c;
            mp1[b]+=c;
        }mp.clear();
        for(ll i=0;i<=2*n;i++)g1[i].clear();
        ll ans=0;
        for(ll i=1;i<=2*n;i++)ans=max(ans, mp1[i]), mp1[i]=0;
        //sort(g.begin(), g.end());
        //reverse(g.begin(), g.end());
        for(ll i=0;i<g.size();i++){
            if(mp[{g[i].sc.fr, g[i].sc.sc}]!=0){ans=max(ans, mp[{g[i].sc.fr, g[i].sc.sc}]+g[i].fr);}
            if(mp[{g[i].sc.sc, g[i].sc.fr}]!=0){ans=max(ans, mp[{g[i].sc.sc, g[i].sc.fr}]+g[i].fr);}
            for(ll j=0;j<g1[g[i].sc.fr].size();j++){
                mp[{g[i].sc.sc, g1[g[i].sc.fr][j].sc}]=g[i].fr+g1[g[i].sc.fr][j].fr;
            }
            for(ll j=0;j<g1[g[i].sc.sc].size();j++){
                mp[{g[i].sc.fr, g1[g[i].sc.sc][j].sc}]=g[i].fr+g1[g[i].sc.sc][j].fr;
            }g1[g[i].sc.sc].clear();
            g1[g[i].sc.fr].clear();
            g1[g[i].sc.fr].pb({g[i].fr, g[i].sc.sc});
            g1[g[i].sc.sc].pb({g[i].fr, g[i].sc.fr});
        }
        cout<<ans<<endl;
    }
}
