#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({second, third});
        values[second]+=third;
        helper[second].push_back({first, third});
    }
    for(ll i = 0; i<2*n; i++) sort(helper[i].begin(), helper[i].end());
    ll maximum = -1;
    for(ll i = 0; i<2*n; i++) maximum=max(maximum, values[i]);
    for(ll i = 0; i<n; i++){
        ll first, second;
        if(helper[rem[i].first].size()<helper[rem[i].second].size()){first = rem[i].first; second=rem[i].second;}
        else{first = rem[i].second; second=rem[i].first;}
        for(auto element : helper[first]){
            if(element.first==second) continue;
            pair<ll, ll> temp = {element.first, -1};
            ll pos = lower_bound(helper[second].begin(), helper[second].end(), temp)-helper[second].begin();
            if(pos==helper[second].size() || helper[second][pos].first!=element.first) continue;
            maximum=max(maximum, save[i]+element.second+helper[second][pos].second);
        }
    }
    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();
}