#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vt vector
vt<vt<int>> p, w;
vt<bool> vis;
vt<int> q;
int ans;
void dfs(int v, int prev1, int prev2, int prev3, int prev4, int prev5){
	
	if(p[v][0]==prev5){
		ans=max(ans, q[prev1]+q[prev2]+q[v]); //clog<<prev1<<" "<<prev2<<" "<<v<<"\n";
	}
	if(!vis[p[v][0]]){
		vis[p[v][0]]=1;
		
		for(auto i:w[p[v][0]]){
			if(i==v) continue;
			dfs(i, prev2, v, prev4, p[v][0], prev3);
		}
	}
	if(p[v][1]==prev5){
		ans=max(ans, q[prev1]+q[prev2]+q[v]); //clog<<prev1<<" "<<prev2<<" "<<v<<"\n";
	}
	if(!vis[p[v][1]]){
		vis[p[v][1]]=1;
	
		for(auto i:w[p[v][1]]){

			if(i==v) continue;
			dfs(i, prev2, v, prev4, p[v][1], prev3);
		}
	}
}
void solve(){
	ans=0; q.clear(); vis.clear(); p.clear(); w.clear();
	int n; cin>>n;
	q.resize(n+1); vis.resize(2*n+1); w.resize(2*n+1); p.resize(2*n+1);
	//clog<<q.size()<<"!!\n";
	for(int i=0; i<n; i++){
		int u, v, d;
		cin>>u>>v>>d;
		q[i]=d;
		p[i].push_back(u); p[i].push_back(v); w[u].push_back(i); w[v].push_back(i);
	}
	//for(auto i:q) cout<<i<<" ";
	for(int i=0; i<n; i++){
		if(!(vis[p[i][0]])){
			dfs(i, -1, -1, -1, -1, -1);
		}
	}
	for(int i=1; i<2*n+1; i++){
		int s=0;
		for(auto j:w[i]){
			s+=q[j];
		}
		ans=max(s, ans);
	}
	cout<<ans<<"\n";
}
signed main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	int t;
	cin>>t;
	while(t--) solve();
}