#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll n, ans, delta[1400001], dist[1400001];

void go()
{
    ans = 0;

    cin>>n;
    for(ll i = 1; i <= n * 2; i++) delta[i] = dist[i] = 0;
    map<pair<ll, ll>, ll> m;
    vector<ll> e[1400001], prew[1400001];
    for(ll i = 1;i <= n; i++) 
    {
        ll v, w, c;
        cin>>v>>w>>c;
        m[{v, w}] += c;
        m[{w, v}] += c;
        e[v].push_back(w);
        e[w].push_back(v);
        delta[v] += c;
        delta[w] += c;
        ans = max(ans, max(delta[v], delta[w]));
    }

    vector<bool> yep(n * 2 + 1, 0);
    for(ll i = 1; i <= n * 2; i++)
    {
        if(!yep[i])
        {
            dist[i] = 1;
            queue<ll> q;
            q.push(i);
            while(!q.empty())
            {
                ll v = q.front();
                yep[v] = 1;
                q.pop();
                for(ll w : e[v])
                {
                    if(!dist[w])
                    {
                        dist[w] = dist[v] + 1;
                        q.push(w);
                        prew[w].push_back(v);
                    }
                    else if(dist[w] >= dist[v])
                    {
                        for(ll pw : prew[w]) 
                        {
                            if(m[{pw, v}]) ans = max(ans, m[{pw, w}] + m[{v, w}] + m[{pw, v}]);
                        }
                        if(dist[w] > dist[v]) prew[w].push_back(v);
                    }
                }
            }
        }
    }

    cout<<ans<<'\n';
    return;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int test_cnt = 1;
    cin>>test_cnt;
    while(test_cnt--) go();
}