#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 7 * 1e5 + 5;
int n, gs[2 * N], a[N], b[N], c[N], ans;
map < pair < int, int >, int > m;
bool used[2 * N];
vector < pair < int, int > > g[2 * N];
void cl()
{
    for (int i = 1; i <= 2 * n; i++)
    {
        gs[i] = 0;
        g[i].clear();
        used[i] = false;
    }
    m.clear();
}
void dfs(int x, int pr, int len)
{
    used[x] = true;
    for (int i = 0; i < g[x].size(); i++)
    {
        if (pr != -1 && g[x][i].first != pr)
        {
            int b1, b2;
            b1 = pr;
            b2 = g[x][i].first;
            if (b1 > b2) swap(b1, b2);
            if (m.count(make_pair(b1, b2)))
            {
                ans = max(ans, len + g[x][i].second + c[m[make_pair(b1, b2)]]);
            }
        }
        if (!used[g[x][i].first])
        {
            dfs(g[x][i].first, x, g[x][i].second);
        }
    }
}
int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    for (int tt = 0; tt < t; tt++)
    {
        cin >> n;
        cl();
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i] >> b[i] >> c[i];
            gs[a[i]] += c[i];
            gs[b[i]] += c[i];
            m[make_pair(a[i], b[i])] = i;
            g[a[i]].push_back(make_pair(b[i], c[i]));
            g[b[i]].push_back(make_pair(a[i], c[i]));
        }
        ans = 0;
        for (int i = 1; i <= 2 * n; i++)
        {
            ans = max(ans, gs[i]);
        }
        for (int i = 1; i <= 2 * n; i++)
        {
            if (!used[i]) dfs(i, -1, 0);
        }
        cout << ans << "\n";
    }
}
