#include <bits/stdc++.h>
#define ll  long long
using namespace std;


int main()
{
    iostream::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        vector<pair<ll,ll>> v;
        map<pair<ll,ll> , ll> mas;
        vector<pair<ll,ll>> mas1[1400005];
        for(ll i=1;i<=n;i++)
        {
            ll a,b,c;
            cin>>a>>b>>c;
            mas1[a].push_back({b,c});
            mas1[b].push_back({a,c});
            mas[{a,b}]=c;
            v.push_back({a,b});
        }
        ll res=0;
        for(ll i=1;i<=n*2;i++)
        {
            ll el=0;
            if(mas1[i].size()==0) continue;
            for(auto [ind,c]:mas1[i])
            {
                el+=c;
            }
            res=max(res,el);
        }
        for(auto [a,b]:v)
        {
            for(ll i=1;i<=2*n;i++)
            {
                if(i!=a&&i!=b) res=max(res , mas[{a,b}]+mas[{min(a,i),max(a,i)}]+mas[{min(b,i),max(b,i)}]);
            }
        }
        cout<<res<<endl;

    }
}
