#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 < int > g[2 * N];
void cl()
{
    for (int i = 1; i <= 2 * n; i++)
    {
        gs[i] = 0;
        g[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];
            g[a[i]].push_back(i);
            g[b[i]].push_back(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 x = 1; x <= 2 * n; x++)
        {
            for (int j = 0; j < g[x].size(); j++)
            {
                int b1, y;
                b1 = g[x][j];
                if (x != a[b1]) y = a[b1];
                else y = b[b1];
                for (int k = 0; k < g[y].size(); k++)
                {
                    int b2, b3, z, xx, zz;
                    b2 = g[y][k];
                    if (b2 == b1) continue;
                    if (y != a[b2]) z = a[b2];
                    else z = b[b2];
                    xx = x;
                    zz = z;
                    if (xx > zz) swap(xx, zz);
                    if (m.count(make_pair(xx, zz)))
                    {
                        b3 = m[make_pair(xx, zz)];
                        if (b3 == b1 || b3 == b2) continue;
                        ans = max(ans, c[b1] + c[b2] + c[b3]);
                    }
                }
            }
        }
        cout << ans << "\n";
    }
}
