#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];
pll m[3007][3007];
void solve(ll g){
    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]);

        m[a[i].first][a[i].second] = {c[i], g};
        Q[a[i].second].push_back({c[i], a[i].first});
    }
    for(int i=1; i<=n; i++){
        ll target = 0;
        ll lock = 0;
        for(auto x : Q[a[i].first]){
            target = x.first;
            lock = 0;

            for(auto y : Q[a[i].second]){
                if(y.second == x.second){
                    lock = y.first;
                    break;
                }
            }

            ans = max(ans, target + c[i] + lock);
        }
    }
    cout << ans << endl;
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t;
    cin >> t;
    ll cnt = 0;
    while(t--){
        cnt++;
        solve(cnt);
    }
    return 0;
}