#include <bits/stdc++.h>
using namespace std;

map<pair<long long, long long>,long long> m;

struct g{
    long long cost,n1=-1,n2=-1;
};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long t;
    cin>>t;
    for(long long pou=0; pou<t; pou++){

    int n;
    cin>>n;
    g girls[2*n+1];
    long long  g1[n],g2[n],c1[n+1];
    c1[n]=0;
    for(long long i=0; i<n; i++){
        long long a,b,c;
        cin>>a>>b>>c;
        c1[i]=c;
        m[{a,b}]=c;
        m[{b,a}]=c;
        girls[a].cost+=c;
        girls[b].cost+=c;
        g1[i]=a;
        g2[i]=b;
        if(c1[girls[a].n1]<c || girls[a].n1==-1){
            girls[a].n2=girls[a].n1;
            girls[a].n1=i;
        }else{
            if(c1[girls[a].n2]<c || girls[a].n2==-1){
                girls[a].n2=i;
            }
        }
        if(c1[girls[b].n1]<c || girls[b].n1==-1){
            girls[b].n2=girls[b].n1;
            girls[b].n1=i;
        }else{
            if(c1[girls[b].n2]<c || girls[b].n2==-1){
                girls[b].n2=i;
            }
        }
    }
     long long ans=0;
    for(long long i=0; i<n; i++){
        ans=max(girls[g2[i]].cost,max(ans,girls[g1[i]].cost));
        //cout<<ans<<endl;
        long long most=n;
        if(girls[g1[i]].n1!=i){
            if(c1[most]<c1[girls[g1[i]].n1])
                most=girls[g1[i]].n1;
        }
        else{
            if(c1[most]<c1[girls[g1[i]].n2] && girls[g1[i]].n2!=-1)
                most=girls[g1[i]].n2;
        }
        if(girls[g2[i]].n1!=i){
            if(c1[most]<c1[girls[g2[i]].n1])
                most=girls[g2[i]].n1;
        }
        else{
            if(c1[most]<c1[girls[g2[i]].n2] && girls[g2[i]].n2!=-1)
                most=girls[g2[i]].n2;
        }
        long long gg1,gg2;
        if(g1[i]==g1[most]){
            gg1=g2[i];
            gg2=g2[most];
        }
        if(g1[i]==g2[most]){
            gg1=g2[i];
            gg2=g1[most];
        }
        if(g2[i]==g1[most]){
            gg1=g1[i];
            gg2=g2[most];
        }
        if(g2[i]==g2[most]){
            gg1=g1[i];
            gg2=g1[most];
        }
        long long fin=m[{gg1,gg2}];
        if(girls[g1[i]].n2!=-1 || girls[g2[i]].n2!=-1){
        ans=max(ans,fin+c1[i]+c1[most]);
        //cout<<ans<<' '<<fin<<' '<<most<<' '<<gg1<<' '<<gg2<<endl;
        }
        //cout<<girls[g2[i]].n1<<endl;
    }
    cout<<ans<<endl;
    }
}
