// #define _GLIBCXX_DEBUG
#pragma GCC optimize("Ofast")
#include <bits/allocator.h>
#pragma GCC target("avx2")
#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<bitset<(int)6e5>> 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][y] = 1;
            a[i] = {{x, y}, z};
            g[y][x] = 1;
            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 b = g[a[i].first.first] & g[a[i].first.second];
            int curr = b._Find_first();
            while (curr < 2 * n) {
                res = max(res, e[{curr, a[i].first.first}] + e[{curr, a[i].first.second}] + a[i].second);
                curr = b._Find_next(curr);
            }
        }
        cout << res << '\n';
    }
}