#include<iostream>
#include<vector>
#include<unordered_map>
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, 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]]));
    }

    for(int i=0; i<n; i++){
        ll sum = c[i];
        unordered_map<ll, ll> g;
        ll mx = 0;
        for(int j=0; j<n; j++){
            if(a[i]==a[j] && i!=j){
                g[b[j]]+=c[j];
                mx=max(mx, g[b[j]]);
            }
            if(b[i]==b[j] && i!=j){
                g[a[j]]+=c[j];
                mx=max(mx, g[a[j]]);
            }
        }

        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;
}