#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[700007], b[700007], c[700007];
vector<ll> v[1400007];
map<ll,ll> mp;
map<pair<ll,ll>,ll> mp2;
ll res = 0;
vector<ll> used;
ll get(ll x, ll y)
{
    return mp2[{min(x,y),max(x,y)}];
}
void dfs(ll x, ll parent)
{
    ll depth = used[x];
    for (int i=0;i<v[x].size();i++)
    {
        ll to = v[x][i];
        if (to==parent) continue;
        if (used[to]==0 || used[to]>(depth+1))
        {
            used[to] = depth+1;
            dfs(to,x);
        }
        else if (used[to]==used[x])
        {
            res = max(res,get(x,parent)+get(x,to)+get(to,parent));
        }
    }
    return ;
}
void bfs(ll x)
{
    priority_queue<vector<ll>, vector<vector<ll>>, greater<vector<ll>>> q;
    q.push({1,x,-1});
    vector<ll> k;
    used[x] = 1;
    while (!q.empty())
    {
        k = q.top();
        q.pop();
        ll depth = k[0], x = k[1], parent = k[2];
        if (depth>used[x]) continue;
        for (int i=0;i<v[x].size();i++)
        {
            ll to = v[x][i];
            if (to==parent) continue;
            if (used[to]==0 || used[to]>(depth+1))
            {
                used[to] = depth+1;
                q.push({depth+1,to,x});
            }
            else if (used[to]==used[x])
            {
                res = max(res,get(x,parent)+get(x,to)+get(to,parent));
            }
        }
    }
    return ;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t;
    cin >>t;
    while (t--)
    {
        ll n;
        cin >>n;

        res = 0;
        mp.clear();
        mp2.clear();
        used.clear();
        for (int i=0;i<=2*n;i++) {used.push_back(0);v[i].clear();}
        for (int i=0;i<n;i++)
        {
            ll x,y,z;
            cin >>x>>y>>z;
            v[x].push_back(y);
            v[y].push_back(x);
            mp2[{x,y}] = z;
            mp[x] += z;
            mp[y] += z;
            a[i] = x;
            b[i] = y;
            c[i] = z;
            res = max(res,mp[x]);
            res = max(res,mp[y]);
        }
        for (int i=1;i<=2*n;i++)
        {
            if (used[i]==0)
            {
                used[i] = 1;
                dfs(i,-1);
            }
        }
        cout <<res<<endl;
    }
}
