#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);
    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);
    }
    /*vector<vector<ll>> adj_boys(n);
    for (auto i: adj){
        for (ll j=0;j<i.size();j++){
            for (ll k=0;k<j;k++){
                adj_boys[j].push_back(k);
                adj_boys[k].push_back(j);
            }
        }
    }*/
   ll mx=0;
    for (ll i=0;i<(1ll<<n);i++){
        vector<ll> curr;
        for (ll j=0;j<n;j++){
            if ((i>>j)&1){
                curr.push_back(j);
            }
        }
        bool good=true;
        ll score=0;
        for (ll j=0;j<curr.size();j++){
            score+=goys[curr[j]].c;
            for (ll k=0;k<j;k++){
                if (goys[curr[k]].grl[0]!=goys[curr[j]].grl[0] &&goys[curr[k]].grl[0]!=goys[curr[j]].grl[1] &&goys[curr[k]].grl[1]!=goys[curr[j]].grl[1] &&goys[curr[k]].grl[1]!=goys[curr[j]].grl[0]){
                    good=false;
                    break;
                }
            }
        }
        if (good){
            chmyax(mx,score);
        }
    }
    cout<<mx<<nl;
}

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