#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;
vector < pair < int, int > > w[2 * N];
void cl()
{
    for (int i = 1; i <= 2 * n; i++)
    {
        gs[i] = 0;
        w[i].clear();
    }
}
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();
        map < pair < int, int >, int > m;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i] >> b[i] >> c[i];
            gs[a[i]] += c[i];
            gs[b[i]] += c[i];
            w[a[i]].push_back(make_pair(b[i], i));
            w[b[i]].push_back(make_pair(a[i], i));
            m[make_pair(a[i], b[i])] = i;
        }
        ans = 0;
        for (int i = 1; i <= 2 * n; i++)
        {
            ans = max(ans, gs[i]);
        }
        for (int i = 1; i <= n; i++)
        {
            int x, y, z, b2, b3;
            x = a[i];
            y = b[i];
            for (int j = 0; j < w[x].size(); j++)
            {
                b2 = w[x][j].second;
                if (b2 == i) continue;
                z = w[x][j].first;
                int yy = y, zz = z;
                if (yy > zz) swap(yy, zz);
                if (m.count(make_pair(yy, zz)))
                {
                    b3 = m[make_pair(yy, zz)];
                    if (b3 == i || b3 == b2) continue;
                    ans = max(ans, c[i] + c[b2] + c[b3]);
                }
            }
        }
        cout << ans << "\n";
    }
}
