#include<iostream>
#include<vector>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;

int t;

void solve(){
    int n;
    cin>>n;
    vector<ll> a(n), b(n), c(n);
    ll ans = 0;
    unordered_map<ll, ll> cnt2;
    unordered_map<ll, unordered_map<ll, ll>> g;
    for(int i=0; i<n; i++){
        cin>>a[i]>>b[i]>>c[i];
        cnt2[a[i]]+=c[i];
        cnt2[b[i]]+=c[i];
        ans=max(ans, max(cnt2[a[i]], cnt2[b[i]]));

        g[a[i]][b[i]]=c[i];
        g[b[i]][a[i]]=c[i];
    }

    for(int i=0; i<n; i++){
        unordered_map<ll, ll> cnt;
        unordered_map<ll, ll> temp1 = g[a[i]];
        unordered_map<ll, ll> temp2 = g[b[i]];
        temp1[b[i]] = 0;
        temp2[a[i]] = 0;

        ll sum = c[i];
        ll mx = 0;

        for(const auto& it : temp1){
            cnt[it.first] += it.second;
            mx = max(mx, cnt[it.first]);
        }

        for(const auto& it : temp2){
            cnt[it.first] += it.second;
            mx = max(mx, cnt[it.first]);
        }

        sum += mx;
        ans=max(ans, sum);
    }
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>t;
    while(t--){
        solve();
    }

    return 0;
}