// #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<vector<pair<ll, ll>>> g(2 * n);
        map<ll, ll> c;
        for (ll i = 0; i < n; i++) {
            ll x, y, z;
            cin >> x >> y >> z;
            x--, y--;
            g[x].push_back({y, z});
            g[y].push_back({x, z});
            c[x] += z; c[y] += z;
        }
        ll res = 1;
        for (auto [_, x] : c) res = max(res, x);
        vector<bool> visited(2 * n);
        function<void (ll, pair<ll, ll>, pair<ll, ll>)> dfs = [&](ll i, pair<ll, ll> p, pair<ll, ll> p2) {
            if (visited[i]) return;
            visited[i] = 1;
            for (auto [j, x] : g[i]) {
                if (j == p2.first) res = max(res, x + p.second + p2.second);
                if (!visited[j]) dfs(j, {i, x}, p);
            }
        };
        for (ll i = 0; i < 2 * n; i++)
            dfs(i, {-1, -1}, {-1, -1});
        cout << res << '\n';
    }
}