#include <bits/stdc++.h>
//#define endl '\h'
#define se second
#define fi first




typedef long long ll;
//typedef pair<ll,ll> pll;






using namespace std;




ll n,m,res;
const long long dim =700002;
map<long long, long long> mpp;
map<pair<ll,ll>, ll> mpl;
set<ll> s[dim*2],cl;
set<pair<ll,ll>> spl;
void solve(){
    ll a,b,c;



// S HAVE TO BE CLEARED
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> a >> b >> c;
        mpl[{a,b}]+=c;
        spl.insert({a,b});
        mpp[a]+=c;
        mpp[b]+=c;
        s[a].insert(b);
        cl.insert(a);

    }
    res=0;
    for(auto tm:spl){
        a=tm.fi;
        c=tm.se;
        res=max(max(mpp[a],mpp[c]),res);
//        tmpRoad=-1;
        for(auto to:s[a]){

            if(s[to].find(c)!=s[to].end()){res=max(mpl[{a,c}]+mpl[{a,to}]+mpl[{to,c}],res);break;}
        }


      //  cout << mpp[tm] << endl;
    }
    for(auto i:cl)s[i].clear();
    cl.clear();
    spl.clear();
    mpp.clear();
    mpl.clear();
    cout << res << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
