#include <bits/stdc++.h>

using namespace std;

#define int long long

int ans = 0;
map< pair<int, int>, int > mp;

void findCycle(int v, int p, vector<set<int>>&graph, vector<bool>&visited) {
    visited[v] = true;
    for(auto& to : graph[v]) {
        if(v != p && to != p && graph[p].find(to) != graph[p].end()) {
            ans = max(ans, mp[make_pair(v, to)] + mp[make_pair(p, to)] + mp[make_pair(v, p)]);
        }
        if(!visited[to]) {
            findCycle(to, v, graph, visited);
        }
    }
}

void solve() {
    ans = 0;
    int n;
    cin >> n;
    vector<set<int>> graph(2*n+1);
    vector<bool>visited(2 * n + 1);
    vector<int> vc(2 * n + 100);
    vector<int> vt;
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        mp[make_pair(a, b)] = c;
        mp[make_pair(b, a)] = c;
        vc[a] += c;
        vc[b] += c;
        graph[a].insert(b);
        graph[b].insert(a);
        vt.emplace_back(a);
        vt.emplace_back(b);
    }
    ans = *max_element(vc.begin(), vc.end());
    for(auto& vx : vt) {
        if(!visited[vx]) {
            findCycle(vx, vx, graph, visited);
        }
    }
    cout << ans << '\n';
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    cin >> tt;
    while(tt--) {
        solve();
    }
}
