
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < ll, ll > pii;
typedef pair < ld, ld > pdd;

const ll DIM = 1500007;
const ll INF = 1e18;
const ld eps = 0.00000001;
const ld gr = (1 + sqrt(5)) / 2;

ll binSearch(vector < ll >& vec, ll key) {
    ll L = 0, R = vec.size() + 1;
    while(R - L > 1) {
        ll mid = (L + R) / 2;
        if(vec[mid] <= key) L = mid;
        else R = mid;
    }
    return L;
}

ll n;
ll res[DIM];
ll tmp[DIM];
bool used[DIM];
vector < ll > x;
vector < pii > v[DIM];

void solve() {
    ll n;
    cin >> n;
    ll ans = -INF;
    for(int i = 1; i <= n; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
        res[a] += c;
        res[b] += c;

        if(!used[a]) {
            used[a] = true;
            x.push_back(a);
        }
        if(!used[b]) {
            used[b] = true;
            x.push_back(b);
        }
    }

    for(auto st: x) {
        ans = max(ans, res[st]);
        for(auto to1: v[st]) tmp[to1.first] = to1.second;
        for(auto to1: v[st]) {
            for(auto to2: v[to1.first]) {
                if(tmp[to2.first] != 0) {
                    ans = max(ans, to1.second + to2.second + tmp[to2.first]);
                }
            }
        }
        for(auto to1: v[st]) tmp[to1.first] = 0;
        res[st] = 0;
        used[st] = false;
    }
    cout << ans << endl;
    for(auto st: x) {
        v[st].clear();
    }
    x.clear();
}

int main()
{

    ll T;
    cin >> T;
    while(T--) solve();
    return 0;
}
