#include<bits/stdc++.h>
#define ll int
#define ff first
#define ss second
#define mkp make_pair
#define pb push_back
using namespace std;
#pragma GCC optimize("O3","Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        pair<pair<ll,ll>,ll> p[n+2];
        vector<ll> v[2*n+2];
        map<pair<ll,ll>,ll> mp;
        for(ll i=1; i<=n; i++)
        {
            cin>>p[i].ff.ff>>p[i].ff.ss>>p[i].ss;
            if(p[i].ff.ff>p[i].ff.ss){swap(p[i].ff.ss,p[i].ff.ff);}
            v[p[i].ff.ff].pb(i);
            v[p[i].ff.ss].pb(i);
            mp[{p[i].ff.ff,p[i].ff.ss}]+=p[i].ss;
        }
        ll ans=-1;
        //sort(p+1,p+n+1);
        for(ll i=1; i<=2*n; i++)
        {
            ll res=0;
            if(v[i].empty()){continue;}
            vector<ll> v2;
            v2.pb(-1);
            v2.pb(-1);
            v2.pb(-1);
            for(auto q:v[i])
            {
              pair< pair<ll,ll>,ll> x=p[q];
              if(v2[0]==-1 && i!=x.ff.ss && v2[0]!=x.ff.ss && v2[1]!=x.ff.ss){v2[0]=x.ff.ss;}
              else if(v2[0]==-1 && i!=x.ff.ff && v2[0]!=x.ff.ff && v2[1]!=x.ff.ff){v2[0]=x.ff.ff;}

              else if(v2[1]==-1 && i!=x.ff.ss && v2[0]!=x.ff.ss && v2[1]!=x.ff.ss){v2[1]=x.ff.ss;}
              else if(v2[1]==-1 && i!=x.ff.ff && v2[0]!=x.ff.ff && v2[1]!=x.ff.ff){v2[1]=x.ff.ff;}

              else if(v2[2]==-1 && i!=x.ff.ss && v2[0]!=x.ff.ss && v2[1]!=x.ff.ss){v2[2]=x.ff.ss;}
              else if(v2[2]==-1 && i!=x.ff.ff && v2[0]!=x.ff.ff && v2[1]!=x.ff.ff){v2[2]=x.ff.ff;}
              res+=x.ss;
            }
            if(v2[0]==-1 || v2[1]==-1 || v2[2]!=-1 ){ans=max(ans,res); continue;}
            ll k=v2[0],k2=v2[1];
            res=res+mp[{k,k2}]+mp[{k2,k}];
            ans=max(ans,res);
        }
        cout<<ans<<'\n';
    }
}
