#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
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]);
    }

    bool sameSet(int a, int b) {
        a = findParent(a);
        b = findParent(b);
        return a == b;
    }

    void unionSets(int a, int b) {
        a = findParent(a);
        b = findParent(b);
        if (a == b) return;
        if (h[a] < h[b]) {
            swap(a, b);
        }
        p[b] = a;
        h[a] += h[b];
    }
};

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

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


        n *= 2;

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

        for (int i = 0; i < n / 2; i++) {
            ll a, b, c;
            cin >> a >> b >> c;
            adj[a].push_back({b, c});
            adj[b].push_back({a, c});
            edge[a * (n + 10) + b] = c;
            edge[b * (n + 10) + 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.find(adj[node][i].first * (n + 10) + adj[node][j].first) != edge.end()) {
                        ll here = adj[node][i].second +
                         adj[node][j].second +
                          edge[adj[node][i].first * (n + 10) + 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.find(heavy[i] * (n + 10) + heavy[j]) != edge.end()) {
                    for (int k = j + 1; k < heavy.size(); k++) {
                        if (edge.find(heavy[i] * (n + 10) + heavy[k]) != edge.end() &&
                             edge.find(heavy[j] * (n + 10) + heavy[k]) != edge.end()) {
                            ll here = edge[heavy[i] * (n + 10) + heavy[j]] + edge[heavy[i] * (n + 10) + heavy[k]] +
                                edge[heavy[j] * (n + 10) + heavy[k]];
                            ans = max(ans, here);
                        }
                    }
                }
            }
        }

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

    
}