#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define int long long
using namespace std;

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;
        vector<vector<int>>v;
        vector<int>v1;
        v1.resize(n*2+1);
        v.resize(n*2+1);
        int mx = 0;
        map<pair<int,int>,int>mp;
        pair<pair<int,int>,int>a[n];
        for(int i = 0;i<n;i++){
            cin>>a[i].first.first>>a[i].first.second>>a[i].second;
            mp[make_pair(a[i].first.first,a[i].first.second)] = a[i].second;
            mx = max(mx,a[i].second);
            v1[a[i].first.first]+=a[i].second;
            v1[a[i].first.second]+=a[i].second;
            mx = max(mx,max(v1[a[i].first.first],v1[a[i].first.second]));
        }
        sort(a,a+n);
        for(int i = 0;i<n;i++){
            int l = 0,r = 0;
            while(l < v[a[i].first.first].size() && r < v[a[i].first.second].size()){
                if(v[a[i].first.first][l] < v[a[i].first.second][r]){
                    l++;
                }
                else if(v[a[i].first.first][l] > v[a[i].first.second][r]){
                    r++;
                }
                else{
                    mx = max(mx,mp[make_pair(v[a[i].first.first][l],a[i].first.first)]+mp[make_pair(v[a[i].first.first][l],a[i].first.second)]+a[i].second);
                    l++;
                }
            }
            v[a[i].first.second].push_back(a[i].first.first);
        }
        cout<<mx<<endl;
    }
}
