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

typedef long long ll;

ll n, ans, delta[700001];
map<ll, map<ll, ll>> e;
bool pw[700001];

void dfs(ll v, ll prew)
{
    pw[v] = 1;
    for(auto i : e[v]) {
        ll w = i .first;
        if(!pw[w])
            dfs(w, v);
        else if(e[prew][w] && e[v][w] && e[prew][v]){
            ans = max(ans, e[prew][w] + e[v][w] + e[prew][v]);
            //cout<<e[prew][w] + e[v][w] + e[prew][v]<<'+'<<v<<"+\n";
        }
    }
    return;
}

void go()
{
    ans = 0;
    e.clear();

    cin>>n;
    for(ll i = 1; i <= n * 2; i++) pw[i] = delta[i] = 0;
    for(ll i = 1;i <= n; i++) 
    {
        ll v, w, c;
        cin>>v>>w>>c;
        e[v][w] += c;
        e[w][v] += c;
        delta[v] +=c;
        delta[w] += c;
        ans = max(ans, max(delta[v], delta[w]));
    }

    for(ll i = 1; i <= n; i++) dfs(i, -1);

    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();
}
