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

    ll t;
    cin >>t;
    while (t--)
    {
        ll n;
        cin >>n;
        map<ll,ll> mp;
        ll res = 0;
        map<pair<ll,ll>,ll> mp2;
        for (int i=0;i<n;i++)
        {
            ll x,y,z;
            cin >>x>>y>>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]);
        }
        for (int i=0;i<n;i++)
        {
            for (int j=i+1;j<n;j++)
            {
                if (a[i]==a[j])
                {

                    res = max(res,c[i]+c[j]+mp2[{min(b[i],b[j]),max(b[i],b[j])}]);
                }
            }
        }
        cout <<res<<endl;
    }
}
