#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long LL;

struct S
{
    int a, b, y;
};

pair<int,int> MP(int a, int b)
{
    if (a < b) return {a,b};
    else return {b,a};
}

void solve()
{
    int n;
    cin >> n;

    map<int, LL> M;
    map<pair<int,int>, LL> M2;
    map<int, vector<int>> G;

    vector<vector<int>> vec(n, vector<int>(3));
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<3; j++)
        {
            cin >> vec[i][j];
        }

        if (vec[i][0] > vec[i][1]) swap(vec[i][0], vec[i][1]);

        auto iter = G.find(vec[i][0]);
        if (iter != G.end())
        {
            iter->second.push_back(vec[i][1]);
        }
        else
        {
            G[vec[i][0]] = {vec[i][1]};
        }

        M2[{vec[i][0], vec[i][1]}] = vec[i][2];
        M[vec[i][0]] += vec[i][2];
        M[vec[i][1]] += vec[i][2];
    }

    LL mx=0;
    
    // cout << "MAP:\n";
    // for (auto p : M)
    // {
    //     cout << p.first << " " << p.second << endl;
    // }
    // cout << "END\n";

    map<int, LL>::iterator iter;

    for (int i=0; i<n; i++)
    {
        
        LL a = vec[i][0];
        LL b = vec[i][1];
        LL c = vec[i][2];

        LL cur1 = c;
        LL cur2 = c;

        iter = M.find(a);
        if (iter != M.end())
        {
            cur1 += iter->second - c;
        }
        
        iter = M.find(b);
        if (iter != M.end())
        {
            cur2 += iter->second - c;
        }

        LL cur3 = c;
        auto vec = G[a];
        for (LL y : vec)
        {
            LL a1 = a;

            LL b1 = b;

            pair<LL, LL> p1 = MP(a1, y);
            pair<LL, LL> p2 = MP(b1, y);

            auto iter1 = M2.find(p1);
            auto iter2 = M2.find(p2);
        
            if (iter1 != M2.end() && iter2 != M2.end())
            {
                cur3 = max(cur3, (iter1->second + iter2->second + c));
            }
        }

        // cout << "I:\t" << i << " | CUR:\t" << cur1 << ' ' << cur2 << ' ' << cur3 << endl;
        mx = max(mx, max(cur3, max(cur1, cur2)));

        // cout << "(I=" << i << ")\t|\t" << "(" << a << ", " << b << ")\t" << cur << endl;
    }

    cout << mx << endl;

}

int main()
{
    int t;
    cin >> t;
    while (t--) solve();
}