#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define lf long double
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define nl "\n"
#define chmyax(a,b) a=max(a,b)
#define chmyin(a,b) a=min(a,b)

struct boy{
    vector<ll> grl;
    ll c;
};

void solve(){
    ll a,b,c,d,e,f,g;
    ll n,m;
    cin>>n;
    vector<vector<ll>> adj(2*n);
    vector<boy> goys(n);
    ll mx=0;
    for (ll i=0;i<n;i++){
        cin>>a>>b>>c;
        --a;--b;
        goys[i]={{a,b},c};
        adj[a].push_back(i);
        adj[b].push_back(i);
    }
    function<ll(ll)> cnt_score = [&](ll initial){
        set<ll> visited;
        set<ll> inserted;
        ll cnt=0;
        priority_queue<ll> pq;
        pq.push(initial);
        visited.insert(initial);
        while (!pq.empty()){
            ll curr = pq.top();
            pq.pop();
            bool good=true;
            for (auto k: inserted){
                if (goys[k].grl[0]!=goys[curr].grl[0] &&goys[k].grl[0]!=goys[curr].grl[1] &&goys[k].grl[1]!=goys[curr].grl[1] &&goys[k].grl[1]!=goys[curr].grl[0]){
                    good=false;
                    break;
                }
            }
            if (!good)continue;
            inserted.insert(curr);
            cnt+=goys[curr].c;
            for (ll i: goys[curr].grl){
                for (ll j: adj[i]){
                    if (visited.count(j))continue;
                    pq.push(j);
                    visited.insert(j);
                }
            }
        }
        return cnt;
    };
    for (ll i=0;i<n;i++){
        chmyax(mx,cnt_score(i));
    }
    cout<<mx<<nl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll a,b,c,d,e,f,g;
    ll t;
    cin>>t;
    while (t--){
        solve();
    }
}