#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long;

void handle_test() {
    int n;
    cin >> n;

    vector<vector<pair<int, ll>>> edges(n * 2, vector<pair<int, ll>>{});

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

        a -= 1;
        b -= 1;

        edges[a].push_back({ b, c });
        edges[b].push_back({ a, c });
    }

    vector<int> order(n * 2, -1);
    vector<int> parents(n * 2, -1);
    vector<int> parent_c(n * 2, -1);

    ll res = 0;

    auto handle_triangle = [&](ll c1, ll c2, ll c3) {
        res = max(res, c1 + c2 + c3);
    };

    function<void(int)> dfs = [&](int start) {
        int parent = parents[start];
        order[start] = parent == -1 ? 0 : order[parent] + 1;

        for (auto [to, c] : edges[start]) {
            if (order[to] == -1) {
                parents[to] = start;
                parent_c[to] = c;
                dfs(to);
            } else if (order[to] == order[start] - 2 && order[start] >= 2) {
                handle_triangle(c, parent_c[start], parent_c[parent]);
            }
        }

        order[start] = -2;
    };

    for (int i = 0; i < n * 2; i++) {
        if (order[i] == -1) {
            dfs(i);
        }

        ll sum = 0;

        for (auto [_, c] : edges[i]) {
            sum += c;
        }

        res = max(res, sum);
    }

    cout << res << "\n";
}

int main() {
#ifndef DEBUG
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif

    int tests;
    cin >> tests;

    while (tests--) {
        handle_test();
    }

    return 0;
}
