#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];
        vector<pair<ll,ll>> 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);
        }
        ll res=0;
        for(auto i:s)
        {
            ll el=0;
            if(mas1[i].size()==0) continue;
            for(auto [ind,c]:mas1[i])
            {
                el+=c;
            }
            ll el1=0;
            res=max(res,el);
            if(mas1[i].size()==1) continue;
            for(int j=mas1[i].size()-1;j<mas1[i].size();j++)
            {
                for(int l=j-1;l<mas1[i].size()-1;l++)
                {
                    auto [a,ca] = mas1[i][j];
                    auto [b,cb] = mas1[i][l];
                    if(a!=b) el1=max(el1,ca+cb+mas[{min(a,b),max(a,b)}]);
                }
            }
            res=max(res,el1);

        }
        cout<<res<<endl;

    }
}
