#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vt vector
vt<vt<int>> p, w;
map<vt<int>, int> wer;
vt<bool> vis;
vt<int> q;
int ans;
void solve(){
	ans=0; q.clear(); vis.clear(); p.clear(); w.clear();
	int n; cin>>n;
	srand(992343238);
	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;
		wer[{u, v}]=i+1;
		p[i].push_back(u); p[i].push_back(v); w[u].push_back(i); w[v].push_back(i);
	}
	for(int j=1; j<=2*n; j++){
		for(int qqq=0; qqq<w[j].size(); qqq++){
			for(int www=qqq+1; www<w[j].size(); www++){
				int x, y;
				int i = w[j][qqq], k = w[j][www];
				if(p[i][0]==p[k][1]){
					x=p[i][1]; y=p[k][0];
				}
				if(p[i][1]==p[k][0]){
					x=p[i][0]; y=p[k][1];
				}
				if(p[i][1]==p[k][1]){
					x=p[i][0]; y=p[k][0];
				}
				if(p[i][0]==p[k][0]){
					x=p[i][1]; y=p[k][1];
				}
				//clog<<x<<" "<<y<<"\n";
				if(wer[{x, y}]!=0){
					//clog<<q[wer[{x, y}]-1]+q[i]+q[k]<<"!!!\n";
					ans=max(ans, q[wer[{x, y}]-1]+q[i]+q[k]);
				}
			}
		}
	}
	for(int i=1; i<=2*n; 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();
}