#include <bits/stdc++.h>

using namespace std;

#define el '\n'
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define ppll pair<pll, ll>
#define ff first
#define ss second
#define all(x)  x.begin(), x.end()
#define pb push_back

const ll DIM = 7e5 + 7;

vector<pll> a[2*DIM];

ll v1[DIM], v2[DIM], c[DIM];
ll k[2*DIM], vis[2*DIM];
ll st, answer;

void dfs (ll v, ll d, ll dist) {

    //cout << "/" << v << " " << d << " " << dist << el;

    vis[v] = 1;

    for (auto [to, c] : a[v]) {

        //cout << to <<" " << vis[to] << el;

        if (vis[to]) {
            if (to != st) continue;

            if (d == 2) {
                answer = max(answer, dist + c);
                continue;
            }
        }

        else dfs(to, d+1, dist + c);
    }

}

void solve () {

    ll n;
    cin >> n;

    memset(k, 0, sizeof(k));
    for (int i=1; i<=2*n; i++) {
        a[i].clear();
    }

    for (int i=1; i<=n; i++) {

        cin >> v1[i] >> v2[i] >> c[i];

        a[v1[i]].pb({v2[i], c[i]});
        a[v2[i]].pb({v1[i], c[i]});

        k[v1[i]] += c[i];
        k[v2[i]] += c[i];

        answer = max(answer, max(k[v1[i]], k[v2[i]]));
    }

    memset(vis, 0, sizeof(vis));
    for (int i=1; i<=2*n; i++) {
        if (!vis[i]) {
            st = i;
            dfs(i, 0, 0);
        }
    }

    cout << answer << el;

}

int main()
{

    ll ntest;
    cin >> ntest;
    while (ntest--) {
        solve();
    }


    return 0;
}
