#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define all(a) (a).begin(), (a).end()

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

    int tc;
    cin >> tc;
    while (tc--) {
        ll n;
        cin >> n;
        vector<pair<pair<int, int>, ll>> a(n);
        vector<vector<pair<int, ll>>> g(2 * n);
        map<int, ll> c;
        map<pair<int, int>, ll> e;
        ll res = 0;
        for (int i = 0; i < n; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            g[x].push_back({y, z});
            a[i] = {{x, y}, z};
            g[y].push_back({x, z});
            e[{x, y}] = z;
            e[{y, x}] = z;
            c[x] += z, c[y] += z;
            res = max({res, c[x], c[y]});
        }
        for (int i = 0; i < n; i++) {
            auto [x, y] = a[i].first;
            if (g[x].size() > g[y].size()) swap(x, y);
            for (auto [z, v] : g[x]) {
                if (e.count({z, y})) res = max(res, a[i].second + e[{z, y}] + v);
            }
        }
        cout << res << '\n';
    }
}