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

struct node {
    ll to, c, i;
};

vector<node> a[2*DIM];
vector<ll> v2[2*DIM];
ll k[2*DIM], vis[2*DIM], tin[2*DIM], tmin[2*DIM], bridge[DIM];
ll r, timer, nodes, cnt;

void dfs1 (ll v, ll edge) {
    //cout << "/" << v << el;
    vis[v] = 1;
    tin[v] = tmin[v] = ++timer;

    for (auto [to,c,e] : a[v]) {
        if (e == edge) continue;

        if (vis[to]) tmin[v] = min(tmin[v], tin[to]);
        else {
            dfs1(to,e);

            tmin[v] = min(tmin[v], tmin[to]);

            if (tmin[to] > tin[v]) {
                bridge[e] = 1;
            }
        }
    }
}

void dfs2 (ll v) {
    //cout << "/" << v << " " << cnt << el;
    vis[v] = 1;
    nodes++;
    for (auto [to, c, e] : a[v]) {

        if (bridge[e]) continue;

        cnt += c;
        r++;

        if (!vis[to] && !bridge[e]) {
            dfs2(to);
        }
    }
}

void solve () {

    ll n;
    cin >> n;
    ll answer = 0;

    memset(k, 0, sizeof(k));
    memset(tin, 0, sizeof(tin));
    memset(tmin, 0, sizeof(tmin));
    memset(vis, 0, sizeof(vis));
    memset(bridge, 0, sizeof(bridge));
    for (int i=1; i<=n; i++) {
        a[i].clear();
    }

    for (int i=1; i<=n; i++) {
        ll v1, v2, c;
        cin >> v1 >> v2 >> c;

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


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

  //      cout << "/" << a << " " << b << " " << k[a] << " " << k[b] << el;
//
        answer = max(answer, max(k[v1], k[v2]));
    }

    for (int i=1; i<=2*n; i++) {
        if (!vis[i]) {
            dfs1(i, -1);
        }
    }
    //cout << 1 << el;

    /*for (int i=1; i<=n; i++) {
        cout << bridge[i] << " ";
    }
    cout << el;*/

    memset(vis, 0, sizeof(vis));
    for (int i=1; i<=2*n; i++) {
        if (!vis[i]) {
            dfs2(i);
            //cout <<"&" <<cnt << " " << r << " " << nodes << " " << el;

            if (r/2 == (nodes*(nodes-1))/2) answer = max(answer, cnt/2);
            r = cnt = nodes = 0;

            //cout << "/" << i << " " << answer << el;
        }
    }



    cout << answer << el;

}

int main()
{

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


    return 0;
}
