#include <bits/stdc++.h>

using namespace std;
long long x[16*1000000];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long t;
    cin>>t;
    while(t--){
        long long n, q, w, e, ans=0, l, m, r;
        cin>>n;
        unordered_map<long long, long long> mp;
            vector<pair<long long, long long>>pr[2*n+1], nx[2*n+1];
        long long a[n];
        for(long long i=1;i<=2*n;i++){
            x[i]=0;
        }
        for(long long i=0;i<n;i++){
            cin>>q>>w>>e;
            pr[w].push_back(make_pair(q, e));
            nx[q].push_back({w, e});
            x[q]+=e;
            x[w]+=e;
            mp[q*1000000000+w]=e;
        //cout<<"!"<<endl;
        }
        //return 0;
        for(long long i=2;i<2*n;i++){
            if(!pr[i].empty())sort(pr[i].begin(), pr[i].end());
            if(!nx[i].empty())sort(nx[i].begin(), nx[i].end());
        }
        for(auto it:mp){
            l=0;
            q=(it.first)/1000000000;
            w=it.first%1000000000;
            for(long long i=0;i<nx[q].size();i++){
                while(l<pr[w].size()&&pr[w][l].first<nx[q][i].first)l++;
                if(l==pr[w].size())break;
                if(pr[w][l].first==nx[q][i].first){
                    ans=max(ans, pr[w][l].second+nx[q][i].second+it.second);
                }
            }
        }
        //cout<<ans;
        for(long long i=1;i<=2*n;i++){
            ans=max(ans, x[i]);
        }
        cout<<ans<<"\n";
    }
    return 0;
}
