#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;

ll df[clasik];

ll cnt[2*clasik];

mt19937 vira(228);

vector <ll> per[2*clasik];
set <ll> rev[2*clasik];

ll cherez[2*clasik];

void solve() {

    ll n;

    cin >> n;

    ll aa , b ,c;

    map< pair<int,int> , ll > used; 

    for (int i = 1 ; i<=n ; i++) {
        cin>>aa>>b>>c;
        cnt[aa] += c;
        cnt[b]+=c;
        per[aa].pb(b);
        rev[b].insert(aa);
        used[{aa , b}]+=c;
    }

    ll res = 0;

    for (int i = 1 ; i<=2*n ; i++) {
        res = max(res , cnt[i]);
        for (auto to : per[i]) {
            for (auto to1 : per[to]) {
                if (rev[to1].count(i)) {
                    //cout<<i<<" "<<to<<" "<<to1<<": "<<used[{i , to}]<<" "<<cnt[to] - used[{i,to}]<<" "<< used[{i , to1}]<<endl;
                    res = max(res , cnt[to] + used[{i , to1}]);
                }
            }
        }
    }

    cout<<res<<endl;

    for (int i =1 ; i<=2*n ; i++) {
        rev[i].clear();
        per[i].clear();
        cnt[i] = 0;
    }

}

signed main() {
    
    int t = 1;

    cin>>t;

    while(t--) solve();

    return 0;
}