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

typedef long long LL;

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

    map<LL, LL> M;
    map<pair<LL,LL>, LL> M2;
    set<LL> G;

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

        G.insert(vec[i][0]);
        G.insert(vec[i][1]);

        if (vec[i][0] > vec[i][1]) swap(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<LL, 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;
        for (LL y : G)
        {
            LL a1 = a;

            LL b1 = b;

            pair<LL, LL> p1;
            pair<LL, LL> p2;
            if (a1 > y)
            {
                p1.first = y;
                p1.second = a1;
            }
            else
            {
                p1.first = a1;
                p1.second = y;
            }

            if (b1 > y)
            {
                p2.first = y;
                p2.second = b1;
            }
            else
            {
                p2.first = b1;
                p2.second = 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();
}