#include <bits/stdc++.h>
#define ll long long 
using namespace std;
ll a[700007], b[700007], c[700007];
vector<ll> v[1400007];
map<ll,ll> mp;
map<pair<ll,ll>,ll> mp2;
ll res = 0;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t;
    cin >>t;
    while (t--)
    {
        ll n;
        cin >>n;
        
        res = 0;
        mp.clear();
        mp2.clear();
        for (int i=0;i<=2*n;i++) v[i].clear();
        for (int i=0;i<n;i++)
        {
            ll x,y,z;
            cin >>x>>y>>z;
            v[x].push_back(y);
            v[y].push_back(z);
            mp2[{x,y}] = z;
            mp[x] += z;
            mp[y] += z;
            a[i] = x;
            b[i] = y;
            c[i] = z;
            res = max(res,mp[x]);
            res = max(res,mp[y]);
        }
        ll c2;
        for(int i=0;i<n;i++)
        {
            ll x = a[i], y = b[i];
            if (v[x].size()>v[y].size()) swap(x,y);
            for(int j=0;j<v[x].size();j++)
            {
                c2 = v[x][j];
                if (mp2[{min(y,c2),max(y,c2)}]!=0)
                {
                    res = max(res,c[i]+mp2[{min(x,c2),max(x,c2)}]+mp2[{min(y,c2),max(y,c2)}]);
                }
                
            }
        }
        cout <<res<<endl;
    }
}
