#include <bits/stdc++.h>
#define int long long
using namespace std;

int fac (int n)
{
    int x=1;
    for (int i = 2; i <= n; i++)
    x*=i;

    return x;
}

void solve ()
{
    int n;
    cin>>n;
    vector <pair<pair<int, int>, int>> v(1000);
    int res = 0;

    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].first.first >> v[i].first.second >> v[i].second;
       // res = max (res, v[i].second);
    }
 //   cout << fac(n) << endl;

    for (int i = 0; i <= fac(n)+1000; i++)
    {
        vector <int>y;
        int a = i, x = 1, k = 0;
    //    cout << "K: ";
        while (a >= 1)
        {
            if (a%2 == 1)
            {
                y.push_back(x);
                k+=v[x].second;
            }
            a/=2;
            x++;
        }
      //  cout << " " << "KK: " << k << endl; 
  /*  cout << "Y: ";
        for (auto u : y)
        cout << u << " ";
        cout << endl;
*/
        for (int q = 0; q < y.size(); q++)
        {
            int x1 = v[y[q]].first.first, x2 = v[y[q]].first.second;
            for (int j = q+1; j < y.size(); j++)
            {
                int y1 = v[y[j]].first.first, y2 = v[y[j]].first.second;

                if (x1 == y1 || x2 == y1 || x1 == y2 || x2 == y2)
                    ;
                else
                {
               //     cout << y[q] << " " << y[j] << endl;
                     k=-1;
                     break;
                }
                   
            }
        }
     //   cout << k << endl;

        res = max (res, k);
    }
  
    
    cout << res << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
