
#include <bits/stdc++.h>
#include <random>

using namespace std;

#define ll long long
#define endl '\n'
#define ld long double
#define pb push_back

const ll clasik = 7e5+7;
const ll INF = 1e17;
const ll mod = 1e9+7;
const ll mln = 1e6+111;

ll pref[2*mln];
ll df[mln];

vector <pair<ll,ll> > g[2*clasik] , Tg[2*clasik];

ll cntv[2*clasik];

ll used[2*clasik];
ll res = 0;
void dfs(int v , ll sum) {
    for (auto to : Tg[v]) {
        if (used[to.first] == 2) continue;
        if (used[to.first] == INF) {
            res = max(res , sum+to.second);
            return;
        }
    }
}

void dfs2(int v , ll sum) {
    
    //used[v] = 1;

    for (auto to : g[v]) {
        if (used[to.first] == 2) continue;
        dfs(to.first , to.second + sum);
    }       
}

void dfss(int v) {
    used[v] = INF;
    for (auto to : g[v]) {
        dfs2(to.first , to.second);
    }
}



void solve() {

    ll n;

    cin>>n;
    ll a , b , c;
    for (int i = 1 ; i<=n ; i++) {
        cin>>a>>b>>c;
        g[a].pb({b , c});
        Tg[b].pb({a , c});
        cntv[a]+=c;
        cntv[b]+=c;
    }
    res = 0;
    for (int i = 1 ; i<=2*n ; i++) {
        res = max(res , cntv[i]);
        dfss(i);
        used[i] = 2;
        
    }

    cout<<res<<endl;

    // tut chistim

    for (int i = 1 ; i<=2*n ; i++) {
        used[i] = 0;
        cntv[i] = 0;
        g[i].clear();
        Tg[i].clear();
    }

}

signed main() {
    
    int t = 1;

    cin>>t;

    while(t--) solve();

    return 0;
}