#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;
        map<pair<ll,ll> , ll> mas;
        vector<pair<ll,ll>> mas1[1400005],v;
        unordered_set<ll> s;
        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;
            s.insert(a);
            s.insert(b);
            v.push_back({a,b});
        }
        ll res=0;
        for(auto i:s)
        {
            ll el=0;
            if(mas1[i].size()==0) continue;
            for(auto [ind,c]:mas1[i])
            {
                el+=c;
            }
            res=max(res,el);
            for(ll j=0;j<n;j++)
            {
                auto [a,b]=v[j];
                if(i==a||i==b) continue;
                ll el=mas[{a,b}]+mas[{min(i,a),max(i,a)}]+mas[{min(i,b),max(i,b)}];
                res=max(res,el);
            }
        }
        cout<<res<<endl;

    }
}
