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

void solve(){
    ll a,b,c,d,e,f,g;
    ll n,m;
    cin>>n;
    vector<vector<ll>> adj(2*n);
    vector<boy> goys(n);
    map<pair<ll,ll>,ll> mp;
    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;
    }
    ll mx=0;
    for (ll i=0;i<n;i++){
        chmyax(mx,goys[i].c);
        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].b;
            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();
    }
}