#include <bits/stdc++.h>

using namespace std;

#define int long long

int ans = 0;
map< pair<int, int>, int > mp;
set<int> graph[7 * 100000 + 5];
bool visited[7 * 100000 + 5];

void findCycle(int v, int p) {
    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);
        }
    }
}

void solve() {
    int n;
    cin >> n;
    vector<int> v(2 * n + 1);
    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;
        v[a] += c;
        v[b] += c;
        graph[a].insert(b);
        graph[b].insert(a);
        vt.emplace_back(a);
        vt.emplace_back(b);
    }
    ans = *max_element(v.begin(), v.end());
    for(auto& vx : vt) {
        if(!visited[vx]) {
            findCycle(vx, vx);
        }
    }
    cout << ans << '\n';
    for(auto& i : graph) i.clear();
    memset(visited, false, sizeof(visited));
    ans = 0;
}

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