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

const int DIM = 700005;
vector <int> v[DIM*2];
int mainv;
long long a[DIM], b[DIM], c[DIM];

map <pair<int, int>, long long> mp;
vector <long long> tr;
//vector <int> depth(2*DIM);
bool used[2*DIM];

void dfs(int v1, int par, int par2) {

    used[v1]=1;
/*
if (par!=-1 && par2!=-1) {}*/
    for (auto u1 : v[v1])


    for (auto u1 : v[v1]) {
        if (u1!=par) {
            if (!used[u1])
                dfs(u1, v1, par);
            else 
                if (mp[{ min(u1, par), max(u1, par) }]) {
                    int k1=min(v1, min(par, u1)), k3=max(v1, max(par, u1)), k2 = v1+par+u1-k1-k3;
                    long long ans = mp[{k1, k2}] + mp[{k2, k3}] + mp[{k1, k3}];
                    //cout<<v1<<' '<<par<<' '<<mainv<<' '<<ans<<"!!! ";
                    tr.push_back(ans);
                }

        }    
    }
    return;

}


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

    int t; cin>>t;
    while (t--) {

        int n; cin>>n;

        for (int i=0; i<n; i++) {
            cin>>a[i]>>b[i]>>c[i]; a[i]--; b[i]--;
            mp[{min(a[i], b[i]), max(a[i], b[i])}] = c[i];
        }

        long long ans=0, ans1=0;
        for (int i=0; i<n; i++) ans1 = max(ans1, c[i]);

        long long ans2=0;
        long long k_type[2*n];
        for (int i=0; i<2*n; i++)
            k_type[i]=0;
        for (int i=0; i<n; i++) {
            k_type[a[i]] += c[i];
            k_type[b[i]] += c[i];
        }
        for (int i=0; i<2*n; i++) ans2 = max(ans2, k_type[i]);


        /////////////////////

        long long ans3=0;

        for (int i=0; i<n; i++) {
            v[a[i]].push_back(b[i]);
            v[b[i]].push_back(a[i]);
        }

        for (int i=0; i<2*n; i++) used[i]=0;
        for (int i=0; i<2*n; i++)
            if (!used[i])
                dfs(i, -1, -1);
        sort(tr.begin(), tr.end());
        for (int i=0; i<tr.size(); i++) ans3 = max(ans3, tr[i]);
        //for (auto s : tr) cout<<s<<' ';
        tr.clear();


/**/
        /////////////////////

        ans = max(ans1, ans2);
        ans = max(ans, ans3);
        cout<<ans<<"\n";
        for (int i=0; i<2*n; i++) v[i].clear();
        mp.clear();

    }


}