#include <bits/stdc++.h>
using namespace std;
int t, n, a, b;
long long c, ans, sum1, sum2;
vector<vector<pair<int, long long>>> g(1400001, vector<pair<int, long long>>(0));
vector<bool> Were(1400001);
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    for (int _ = 0; _ < t; ++_) {
        cin >> n;
        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));
        }
        ans = 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;
                sum2 = 0LL; Were[a] = true;
                for (int k = 0; k < g[a].size(); ++k) {
                    b = g[a][k].first; sum2 += g[a][k].second;
                    if (b == i) continue;
                    ans = max(ans, g[i][j].second + g[a][k].second);
                    if (Were[b]) continue;
                    for (int u = 0; u < g[b].size(); ++u) {
                        if (g[b][u].first == i) {
                            ans = max(ans, g[i][j].second + g[a][k].second + g[b][u].second);
                            break;
                        }
                    }
                }
                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;}
    }
}
