#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define lf long double
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define nl "\n"
#define chmyax(a,b) a=max(a,b)
#define chmyin(a,b) a=min(a,b)

struct boy{
    ll a;
    ll b;
    ll c;
};

struct pair_hash{
    size_t operator()(pair<ll,ll> pr) const {
        return hash<ll>()(pr.first)^hash<ll>()(pr.second);
    }
};

const ll MX = 3e5;

void solve(){
    ll a,b,c,d,e,f,g;
    ll n,m;
    cin>>n;
    vector<vector<ll>> adj(2*n);
    vector<ll> sigmascore(2*n);
    vector<boy> goys(n);
    unordered_map<pair<ll,ll>,ll,pair_hash> mp;
    ll mx=0;
    for (ll i=0;i<n;i++){
        cin>>a>>b>>c;
        --a;--b;
        goys[i]={a,b,c};
        adj[a].push_back(b);
        adj[b].push_back(a);
        mp[{a,b}]=c;
        mp[{b,a}]=c;
        sigmascore[a]+=c;
        sigmascore[b]+=c;
        chmyax(mx,sigmascore[a]);
        chmyax(mx,sigmascore[b]);
    }
    vector<bool> visited(2*n,false);
    function<void(ll,ll)> dfs = [&](ll v, ll p){
        for (ll i: adj[v]){
            if (visited[i]){
                if (mp.count({i,p})){
                    mx=max(mx,mp[{i,p}]+mp[{i,v}]+mp[{v,p}]);
                }
                continue;
            }
            visited[i]=1;
            dfs(i,v);
        }
    };
    vector<ll> tovisit(2*n);
    for (ll i=0;i<2*n;i++){
        tovisit[i]=i;
    }
    sort(all(tovisit), [&](ll first, ll second){return adj[first].size()<adj[second].size();});
    for (ll i=0;i<2*n;i++){
        if (visited[tovisit[i]])continue;
        dfs(tovisit[i],tovisit[i]);
    }
    cout<<mx<<nl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("in","r",stdin);
    ll a,b,c,d,e,f,g;
    ll t;
    cin>>t;
    while (t--){
        solve();
    }
}