#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;
    for(int i=1; i<=2*n; i++){
        d[i] = 0;
        Q[i].clear();
    }

    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]);
    }
    sort(a+1, a+1+n);
    for(int i=1; i<=n; i++){
        ll target = 0;
        for(int j=0; j<Q[a[i].first].size(); j++){
            target = Q[a[i].first][j].first;
            ll id = Q[a[i].first][j].second;

            if(Q[id].size() >= j+1){
                target += Q[id][j].first;
                ans = max(ans, target + c[i]);
            }
        }
        Q[a[i].second].push_back({c[i], i});
    }
    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;
}
