#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define mkp make_pair
#define pb push_back
using namespace std;
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll sum[2*n+2]{0};
        pair<ll,pair<ll,ll>> p[n+2];
        ll ans=0;
        for(ll i=1; i<=n; i++)
        {
            cin>>p[i].ss.ff>>p[i].ss.ss>>p[i].ff;
            sum[p[i].ss.ff]+=p[i].ff;
            sum[p[i].ss.ss]+=p[i].ff;
            ans=max({ans,sum[p[i].ss.ff],sum[p[i].ss.ss]});
        }
       // cout<<ans<<'\n';
        sort(p+1,p+n+1);
        for(ll i=n; i>=max(1LL,n-100000); i--)
        {
            //if(rand()%3==0){continue;}
           ll res=0;
           vector<pair<ll,ll>> v;
           res+=p[i].ff;
           v.pb({p[i].ss.ff,p[i].ss.ss});
           for(ll j=n; j>=i+1; j--)
           {
               bool t2=1;
               for(auto x:v)
               {
                  if(x.ff==p[j].ss.ff || x.ss==p[j].ss.ff){continue;}
                  if(x.ff==p[j].ss.ss || x.ss==p[j].ss.ss){continue;}
                  t2=0; break;
               }
               if(!t2){continue;}
               res+=p[j].ff;
               v.pb({p[j].ss.ff,p[j].ss.ss});
           }
           ans=max(ans,res);
        }
        cout<<ans<<'\n';
    }
}