#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;
        pair<pair<int,int>,int>a[n];
        map<int,vector<int>>mp1;
        map<pair<int,int>,int>mp2;
        int mx = 0;
        map<int,int>mp;
        for(int i = 0;i<n;i++){
            cin>>a[i].first.first>>a[i].first.second>>a[i].second;
            mp2[make_pair(a[i].first.first,a[i].first.second)] = a[i].second;
            mp1[a[i].first.first].push_back(a[i].first.second);
            mp1[a[i].first.second].push_back(a[i].first.first);
            mx = max(mx,a[i].second);
            mp[a[i].first.first]+=a[i].second;
            mp[a[i].first.second]+=a[i].second;
            mx = max(mx,max(mp[a[i].first.first],mp[a[i].first.second]));
        }
        for(auto it:mp1){
            if(it.second.size() > 1){
                if(mp2[make_pair(it.second[0],it.second[1])]){
                    mx = max(mx,mp2[make_pair(it.second[0],it.second[1])]+mp[it.first]);
                }
            }
        }
        cout<<mx<<endl;
    }
}
