#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct DSU {
    int n;
    vector<int> p, h;

    DSU(int m) {
        n = m;
        p.assign(n + 1, -1);
        h.assign(n + 1, 1);

        for (int i = 0; i <= n; i++) {
            p[i] = i;
            h[i] = 1;
        }
    }

    int findParent(int node) {
        if (p[node] == node) {
            return node;
        }
        return p[node] = findParent(p[node]);
    }
};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;

        n *= 2;

        vector<pair<int, ll>> adj[n + 1];
        map<pair<int, int>, ll> edge;

        for (int i = 0; i < n / 2; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            adj[a].push_back({b, c});
            adj[b].push_back({a, c});
            edge[{a, b}] = c;
            edge[{b, a}] = c;
        }

        // one node

        ll ans = 0;
        for (int i = 1; i <= n; i++) {
            ll here = 0;
            for (auto ad : adj[i]) {
                here += ad.second;
            }
            ans = max(ans, here);
        }

        // loop
        
        int sq = sqrt(n);

        vector<int> light, heavy;
        light.reserve(n); heavy.reserve(n);
        for (int i = 1; i <= n; i++) {
            if (adj[i].size() < sq) {
                light.push_back(i);
            }
            else {
                heavy.push_back(i);
            }
        }

        for (int node : light) {
            for (int i = 0; i < adj[node].size(); i++) {
                for (int j = i + 1; j < adj[node].size(); j++) {
                    if (edge.count({adj[node][i].first, adj[node][j].first})) {
                        ll here = adj[node][i].second +
                         adj[node][j].second +
                          edge[{adj[node][i].first, adj[node][j].first}];
                        ans = max(ans, here);
                        
                    }
                }
            }
        }

        for (int i = 0; i < heavy.size(); i++) {
            for (int j = i + 1; j < heavy.size(); j++) {
                if (edge.count({heavy[i], heavy[j]})) {
                    for (int k = j + 1; k < heavy.size(); k++) {
                        if (edge.count({heavy[i], heavy[k]}) && edge.count({heavy[j], heavy[k]})) {
                            ll here = edge[{heavy[i], heavy[j]}] + edge[{heavy[i], heavy[k]}] +
                                edge[{heavy[j], heavy[k]}];
                            ans = max(ans, here);
                        }
                    }
                }
            }
        }

        printf("%lld\n", ans);
    }

    
}