#include<iostream>
#include<vector>
#include<unordered_map>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;

int t;

void solve(){
    int n;
    cin>>n;
    vector<ll> a(n), b(n), c(n);
    ll ans = 0;
    unordered_map<ll, vector<pair<ll, ll>>> g;
	unordered_map<ll, ll> cnt2;
    for(int i=0; i<n; i++){
        cin>>a[i]>>b[i]>>c[i];
		cnt2[a[i]]+=c[i];
        cnt2[b[i]]+=c[i];
        ans=max(ans, max(cnt2[a[i]], cnt2[b[i]]));
        g[a[i]].push_back(make_pair(b[i], c[i]));
        g[b[i]].push_back(make_pair(a[i], c[i]));
    }

    for(int i=0; i<n; i++){
		unordered_map<ll, ll> cnt;
		ll sum = c[i];
		ll mx = 0;

		for(int j=0; j<g[a[i]].size(); j++){
			for(int k=0; k < g[g[a[i]][j].first].size(); k++){
				if(g[g[a[i]][j].first][k].first==b[i]) mx = max(g[g[a[i]][j].first][k].second+g[a[i]][j].second, mx);
			}
		}

		sum += mx;

		ans=max(sum, ans);
    }
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>t;
    while(t--){
        solve();
    }

    return 0;
}