
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

using namespace std;
typedef long long ll;
const ll dim=700007;
ll n,a[dim],T[dim],b[dim], c[dim];
string s;
/*ll build(int v, int tl, int tr){
   if(tl==tr){
        T[v]=a[tl];
        return T[v];
   }
   int tm=(tl+tr)>>1;

    T[v]=build(2*v+1, tl, tm)+build(2*v+2, tm+1, tr);
}

ll get(int v, int tl, int tr, int l, int r)
void solve(){
    cin>>n;
}*/
void solve(){
    cin>>n;

    for(int i=1; i<=n; i++)cin>>a[i]>>b[i]>>c[i];


    int maxmask=(1<<n);
  ll   ma=0;

    for(int i=0; i<maxmask; i++){
            //cout << i << endl;
            vector<ll>v;
            ll sum=0;
         for(int j=0; j<n; j++){
            if(i&(1<<j)){v.push_back(j+1); sum+=c[j+1]; }
         }
       bool ok=1;

       for(int j=0; j<v.size(); j++){
        for(int k=j+1; k<v.size(); k++){
            if(a[v[k]]!=a[v[j]] && b[v[k]]!=a[v[j]] && a[v[k]]!=b[v[j]] && b[v[k]]!=b[v[j]])ok=0;
        }
       }
       if(ok)ma=max(ma, sum);
    }

    cout << ma << endl;
}
int main()
{

    int t;
    //t=1;
    cin>>t;
    while(t--)solve();

    return 0;
}
