#include <bits/stdc++.h>
#define ll  long long
using namespace std;

struct st{
    int a,b,c;
};

int main()
{
    iostream::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        map<int,st> mas;
        map<pair<int,int> , int> masc;
        vector<int> mas1[1400005];
        for(int i=1;i<=n;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            st el;
            el.a=a;
            el.b=b;
            el.c=c;
            mas[i]=el;
            masc[{a,b}]=c;
            mas1[a].push_back(i);
            mas1[b].push_back(i);
        }
        int res=0;
        for(int i=1;i<=n*2;i++)
        {
            int el=0;
            for(auto j:mas1[i])
            {
                el+=mas[j].c;
                for(auto l:mas1[i])
                {
                    if(l!=j)
                    {
                        int ind1,ind2;
                        
                        if(mas[j].a==mas[l].a)
                        {
                            ind1=min(mas[j].b,mas[l].b);
                            ind2=max(mas[j].b,mas[l].b);
                        }
                        else if(mas[j].a==mas[l].b)
                        {
                            ind1=min(mas[j].b,mas[l].a);
                            ind2=max(mas[j].b,mas[l].a);
                        }
                        else if(mas[j].b==mas[l].a)
                        {
                            ind1=min(mas[j].a,mas[l].b);
                            ind2=max(mas[j].a,mas[l].b);
                        }
                        else
                        {
                            ind1=min(mas[j].a,mas[l].a);
                            ind2=max(mas[j].a,mas[l].a);
                        }
                        res=max(res,mas[j].c+mas[l].c+masc[{ind1,ind2}]);
                    } 
                }
            }
            res=max(res,el);
        }
        cout<<res<<endl;

    }
}
