#include <bits/stdc++.h>
#define int long long 
using namespace std;
/*mt19937 rng(time(NULL));

int randint(int l, int r)
{
    return uniform_int_distribution(l, r)(rng);
}*/

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int k[2*n+1]{0};
        pair< pair<int, int>, int> a[n+1];
        map<pair<int,int>, int> mp;
        for(int i=1; i<=n; i++)
        {
            int x, b, c;
            cin>>x>>b>>c;
            a[i].first.first=x;
            a[i].first.second=b;
            a[i].second=c;

            mp[{x, b}]=c;
            k[x]+=c;
            k[b]+=c;
        }

        int res=0;
        for(int i=1; i<=2*n; i++)
        {
            res=max(res, k[i]);
        }

       // cout<<res<<" ";

       for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                auto [x, y]=a[i].first;
                auto [x1, y1]=a[j].first;
                
                if(x==x1 && mp[{y, y1}]!=0) 
                {
                    res= max(res, a[i].second+a[j].second+mp[{y, y1}]);
                }
            }
        }

        cout<<res<<"\n";
    }
}
