#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define mkp make_pair
#define pb push_back
using namespace std;
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll sum[2*n+2]{0};
        pair<pair<ll,ll>,ll> p[n+2];
        vector<ll> v[2*n+2];
        map<pair<ll,ll>,ll> mp;
        ll ans=0;
        for(ll i=1; i<=n; i++)
        {
            cin>>p[i].ff.ff>>p[i].ff.ss>>p[i].ss;
            v[p[i].ff.ff].pb(i);
            if(p[i].ff.ss!=p[i].ff.ff){v[p[i].ff.ss].pb(i);}
            mp[{p[i].ff.ff,p[i].ff.ss}]+=p[i].ss;
            sum[p[i].ff.ff]+=p[i].ss;
            sum[p[i].ff.ss]+=p[i].ss;
            ans=max({ans,sum[p[i].ff.ff],sum[p[i].ff.ss]});
        }
        for(ll i=1; i<=2*n; i++)
        {
            ll res=0;
            set<ll> st;
            for(auto q:v[i])
            {
              pair< pair<ll,ll>,ll> x=p[q];
              st.insert(x.ff.ff);
              st.insert(x.ff.ss);
              //cout<<x.ff.ff<<' '<<x.ff.ss<<" NEXT "<<i<<'\n';
              res+=x.ss;
            }
            /*for(auto x:st)
            {
                cout<<i<<' '<<x<<'\n';
            }*/
            if(st.size()!=3){ans=max(ans,res); continue;}
            st.erase(i);
            ll k=*st.begin();
            st.erase(*st.begin());
            ll k2=*st.begin();
            res=res+mp[{k,k2}];
            ans=max(ans,res);
        }
        cout<<ans<<'\n';
    }
}
