#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);
    map<pair<ll, ll>, ll> need;
    for(ll i = 0; i<n; i++){
        ll first, second, third;
        cin >> first >> second >> third;
        first--;
        second--;
        need[{first, second}]=third;
        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]);
    for(ll i = 0; i<2*n; i++){
        ll temp = helper[i].size();
        for(ll j = 0; j<helper[i].size(); j++){
            for(ll k = 0; k<helper[i].size(); k++){
                if(j!=k){
                    //cout << j << ' ' << k << '\n';
                    maximum=max(maximum, helper[i][j].first+helper[i][k].first+need[{helper[i][j].second, helper[i][k].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();
}