#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);
    }
};

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(i);
        adj[b].push_back(i);
        mp[{a,b}]=c;
        mp[{b,a}]=c;
        sigmascore[a]+=c;
        sigmascore[b]+=c;
        chmyax(mx,sigmascore[a]);
        chmyax(mx,sigmascore[b]);
    }
    for (ll i=0;i<n;i++){
        for (ll j: adj[goys[i].a]){
            if (j==i)continue;
            ll other=goys[j].a;
            if (other==goys[i].a)other=goys[j].b;
            mx=max(mx,goys[i].c+goys[j].c+mp[{goys[i].b,other}]);
        }
        for (ll j: adj[goys[i].b]){
            if (j==i)continue;
            ll other=goys[j].a;
            if (other==goys[i].b)other=goys[j].b;
            mx=max(mx,goys[i].c+goys[j].c+mp[{goys[i].a,other}]);
        }
    }
    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();
    }
}