#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll DIM = 2*7e5+7;
vector<pll> Q[DIM];
pll a[DIM];
ll c[DIM], d[DIM];
void solve(){
    ll n;
    cin >> n;
    vector<ll> m[DIM/2];
    for(int i=1; i<=2*n; i++){
        d[i] = 0;
        Q[i].clear();
        m[i].resize(2*n, 0);
    }

    ll ans = 0;
    for(int i=1; i<=n; i++){
        cin >> a[i].first >> a[i].second >> c[i];

        d[a[i].first] += c[i];
        ans = max(ans, d[a[i].first]);

        d[a[i].second] += c[i];
        ans = max(ans, d[a[i].second]);

        m[a[i].first][a[i].second] = c[i];
        Q[a[i].second].push_back({c[i], a[i].first});
    }
    for(int i=1; i<=n; i++){
        ll target = 0;
        for(auto x : Q[a[i].first]){
            target = x.first;
            if(m[x.second][a[i].second] != 0) ans = max(ans, target + c[i] + m[x.second][a[i].second]);
        }
    }
    cout << ans << endl;
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}