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

vector<ll> a[DIM];
vector<ll> a2[DIM];

ll v1[DIM], v2[DIM], c[DIM];
ll k[DIM];

void solve () {

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

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

    for (int i=1; i<=n; i++) {

        cin >> v1[i] >> v2[i] >> c[i];

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

        a[v1[i]].pb(v2[i]);
        a[v2[i]].pb(v1[i]);

        a2[v1[i]].pb(c[i]);
        a2[v2[i]].pb(c[i]);

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

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

    for (int i=1; i<=n; i++) {

        for (int j=0; j<a[v1[i]].size(); j++) {
            ll to = a[v1[i]][j];

            auto it = find(all(a[v2[i]]), to);
            if (it != a[v2[i]].end()) {

                answer = max(answer, c[i] + a2[v1[i]][j] + a2[v2[i]][it - a[v2[i]].begin()]);

            }

        }

    }


    cout << answer << el;

}

int main()
{

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


    return 0;
}
