#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
const ll SIZ = ((ll)7e5 + 5), LONGBOI = ((ll)1e12 + 5);
mt19937_64 REALrandom(chrono::system_clock::to_time_t(chrono::system_clock::now()));

ll n, a, b, c;
ll num[SIZ];
vector<ll> in[SIZ];
map<ll, ll> m[2*SIZ];

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll T;
    cin>>T;
    while(T>0){
        T--;
        cin>>n;
        for(int i=1;i<=2*n;i++) num[i]=0;
        for(int i=1;i<=n;i++){
            cin>>a>>b>>c;
            num[a]+=c;
            m[a][b] = c;
            num[b]+=c;
            m[b][a] = c;
            in[a].push_back(b);
            in[b].push_back(a);
        }
        ll mx=-LONGBOI;
        for(int i=1;i<=2*n;i++){
            if(num[i]>mx){
                mx=num[i];
            }
        }

        for(int we=1;we<=2*n;we++){
            for(int i=0;i<in[we].size();i++){
                for(int j=i+1;j<in[we].size();j++){
                    if(m[i].count(j)){
                        mx = max(mx, m[i][j] + m[we][i] + m[we][j]);
                    }
                }
            }
        }
        cout<<mx<<endl;
    }
    return 0;
}
