#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<ll,ll> sum;
        map<pair<ll,ll> , ll> mas;
        vector<pair<ll,ll>> mas2;
        vector<pair<ll,ll>> mas1[1400005];
        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);
            mas2.push_back({a,b});
            sum[a]+=c;
            sum[b]+=c;
        }
        ll res=0;
        for(auto i:s)
        {
            ll el1=0;
            res=max(res,sum[i]);
            if(mas1[i].size()==1) continue;
            for(auto [a,b]:mas2)
            {
                if(a!=i&&b!=i)
                {
                    int her[3];
                    her[0]=a;
                    her[1]=b;
                    her[2]=i;
                    sort(her,her+3);
                    res=max(res , mas[{her[0],her[1]}]+mas[{her[0],her[2]}]+mas[{her[1],her[2]}]);
                }
            }
        }
        cout<<res<<endl;

    }
}
