#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
void solve(){
    ll n;
    cin >> n;
    ll values[2*n];
    vector<pair<ll, ll>> helper[2*n];
    fill(values, values+2*n, 0);
    ll save[n];
    vector<pair<ll, ll>> rem;
    for(ll i = 0; i<n; i++){
        ll first, second, third;
        cin >> first >> second >> third;
        save[i]=third;
        first--;
        second--;
        rem.push_back({first, second});
        values[first]+=third;
        helper[first].push_back({third, second});
        values[second]+=third;
        helper[second].push_back({third, first});
    }
    ll maximum = -1;
    for(ll i = 0; i<2*n; i++) maximum=max(maximum, values[i]);
    ll girls[2*n];
    fill(girls, girls+2*n, 0);
    for(ll i = 0; i<n; i++){
        vector<ll> temp;
        for(auto element : helper[rem[i].first]){ if(element.second==rem[i].second) continue; girls[element.second]+=element.first; temp.push_back(element.second);}
        for(auto element : helper[rem[i].second]){ if(element.second==rem[i].first) continue; girls[element.second]+=element.first; temp.push_back(element.second);}
        for(auto element : temp){
            maximum=max(maximum, save[i]+girls[element]);
            girls[element]=0;
        }

    }
    cout << maximum << '\n';
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    ll tests;
    cin >> tests;
    while(tests--) solve();
}