#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 = 1e3;

void solve(){
    ll a,b,c,d,e,f,g;
    ll n,m;
    cin>>n;
    vector<bitset<MX>> 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][b]=1;
        adj[b][a]=1;
        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++){
        bitset<MX> tmp =adj[goys[i].a]&adj[goys[i].b];
        for (ll j=tmp._Find_first();j!=MX;j=tmp._Find_next(j)){
            if (j==goys[i].a || j==goys[i].b)continue;
            mx=max(mx,goys[i].c+mp[{goys[i].a,j}]+mp[{goys[i].b,j}]);
        }
    }
    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();
    }
}