#include <bits/stdc++.h>
using namespace std;
int t, n, a, b;
long long c, ans, sum1, sum2, tek;
vector<vector<pair<int, long long>>> g(0, vector<pair<int, long long>>(0));
vector<bool> Were(0);
long long bin_p(int elem) {
    int l = -1, r = g[b].size(), mid;
    while (l < r - 1) {
        mid = (l + r) / 2;
        if (g[b][mid].first <= elem) l = mid; else r = mid;
    }
    if (l != -1 && g[b][l].first == elem) return g[b][l].second;
    return 0LL;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    for (int _ = 0; _ < t; ++_) {
        cin >> n; Were.resize(n * 2); g.resize(n * 2);
        for (int i = 0; i < n; ++i) {
            cin >> a >> b >> c; a--; b--;
            g[a].push_back(make_pair(b, c));
            g[b].push_back(make_pair(a, c));
        }
        for (int i = 0; i < n * 2; ++i) sort(g[i].begin(), g[i].end());
        ans = 0LL; tek = 0LL;
        for (int i = 0; i < n * 2; ++i) {
            if (Were[i]) continue;
            sum1 = 0LL; Were[i] = true;
            for (int j = 0; j < g[i].size(); ++j) {
                ans = max(ans, g[i][j].second);
                sum1 += g[i][j].second;
                a = g[i][j].first; if (Were[a]) continue;
                sum2 = 0LL; tek += g[i][j].second;
                for (int k = 0; k < g[a].size(); ++k) {
                    b = g[a][k].first; sum2 += g[a][k].second;
                    if (Were[b]) continue;
                    tek += g[a][k].second;
                    ans = max(ans, tek + bin_p(i));
                    tek -= g[a][k].second;
                }
                tek -= g[i][j].second;
                ans = max(ans, sum2);
            }
            ans = max(ans, sum1);
        }
        cout << ans << "\n";
        for (int i = 0; i < n * 2; ++i) {g[i].clear(); Were[i] = false;}
    }
}