#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> v[DIM];
vector<ll> v2[DIM];
ll k[DIM];

void solve () {

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

    memset(k, 0, sizeof(k));
    for (int i=1; i<=2*n; i++) {
        v[i].clear();
        v2[i].clear();
    }

    for (int i=1; i<=n; i++) {
        ll a, b, c;
        cin >> a >> b >> c;

        v[a].pb(b);
        v[b].pb(a);

        v2[a].pb(c);
        v2[b].pb(c);

        k[a] += c;
        k[b] += c;

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

    for (int i=1; i<=2*n; i++) {
        for (int j=0; j<v[i].size(); j++) {
            ll to = v[i][j];
            //cout << "*" << i << " " << to << " " << v[to].size() << " " << v[to][0] << el;
            for (int m=0; m<v[to].size(); m++) {
                ll to2 = v[to][m];

                //cout << "!" << i << " " << to << " " << to2 << el;

                if (to2 == i) continue;



                auto it = find(all(v[i]), to2);
                if (it != v[i].end()) {
                    ll c = v2[i][j];
                    ll c2 = v2[to][m];

                    //cout << "/" << i << " " << to << " " << to2 << " " << c+c2+ v2[i][(it - v[i].begin())] <<  " " << v2[i][(it - v[i].begin())] << el;

                    answer = max(answer, c+c2+ v2[i][it - v[i].begin()]);
                }
            }
        }
    }

    cout << answer << el;

}

int main()
{

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


    return 0;
}
