/*IN FISH AT EGOI I TRUST*/
/*IN FISH AT EGOI I TRUST*/
/*IN FISH AT EGOI I TRUST*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppl;
typedef long double ld;

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt")
int mod,tt,v1,v2,d,n,bs,o[1500009];
ll g[1500009];
ll p(int x,int y){
    if(x>y) swap(x,y);
   if(mp[x].count(y)) return mp[x][y];
   else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    mod=1e9+7;
    cin>>tt;
    for(int oo=1;oo<=tt;oo++){
        cin>>n;
        bs=6000;
        vector<pair<int,int>> A[2*n+3],B[2*n+3];
vector<pair<pair<int,int>,int>> D;
unordered_map<int,ll> mp[2*n+3];

        for(int i=1;i<=n;i++) {
            cin>>v1>>v2>>d;
            g[v1]+=d;
            g[v2]+=d;
            if(v1>v2) swap(v1,v2);
            o[v1]++;
            o[v2]++;
            D.push_back({{v1,v2},d});
        }
        ll res=0;
        for(int i=1;i<=2*n;i++) res=max(res,g[i]);
        for(auto x:D) {
            v1=x.first.first; v2=x.first.second; d=x.second;
            if(o[v1]<bs ) A[v1].push_back({v2,d});
            else if(o[v1]>=bs && o[v2]<bs) A[v2].push_back({v1,d});
            else if(o[v1]>=bs && o[v2]>=bs) B[v1].push_back({v2,d});
            mp[v1][v2]=d;
        }

        for(int i=1;i<=2*n;i++) {
                if(o[i]<bs) {
                    for(int j=0;j<A[i].size();j++) for(int h=j+1;h<A[i].size();h++) {
                            res=max(res,A[i][j].second+A[i][h].second+p(A[i][j].first,A[i][h].first));
                    }
                }
                else{
                    for(int j=0;j<B[i].size();j++) for(int h=j+1;h<B[i].size();h++) {
                            res=max(res,B[i][j].second+B[i][h].second+p(B[i][j].first,B[i][h].first));
                    }
                }
        }


        
        cout<<res<<"\n";
    }

    return 0;
}
