#include <bits/stdc++.h>
using namespace std;
vector <pair<pair<int, int>, int>> v(1000);
 set <int> s;
 map <int, int> mp;
 vector <int> vec;
 int sum = 0,n;
void rec (int i)
{
    mp[i]++;
    vec.push_back(i);
    s.insert(v[i].first.first);
    s.insert(v[i].first.second);
    sum += v[i].second;

      for (int  j = 1; j <= n; j++)
        {
            if (mp[j] > 0)
            continue;

            if (s.find (v[j].first.first) != s.end() || s.find(v[j].first.second) != s.end())
            {
                rec(j);
            }

        }
}

void solve ()
{
    cin>>n;

    int res = 0;

    v.clear();

    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 << res << endl;
    vector <int> y;

    for (int i = 1; i <= n; i++)
    {
        s.clear();
        mp.clear();
        vec.clear();
        sum=0;
        rec(i);
       // cout << i << " " << sum << endl;
        if (sum > res)
        {
            res = sum;
            y=vec;
        }
    }

    /* for (auto f : y)
        cout << f << " ";
    cout << endl;

*/
    for (int i = 0; i < y.size(); i++)
    {
        if (y[i] == -1)
        continue;

        int x1 = v[i].first.first, x2 = v[i].first.second;

        for (int j = 0; j < y.size(); j++)
        {
            if (y[j] == -1)
                continue;
            if (y[i] == -1)
                break;
            int y1 = v[j].first.first, y2 = v[j].first.second;

            if (x1 == y1 || x2 == y1 || x1 == y2 || x2 == y2)
            {
                continue;
            }
            else
            {
                if (v[i].second < v[j].second)
                {
                    y[i]=-1;
                    res -= v[i].second;
                }
                else
                {
                    y[j]=-1;
                    res -= v[j].second;
                }

            }
        }
    }
/*
    for (auto f : y)
        cout << f << " ";
    cout << endl;

*/
    cout << res << endl;
}

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

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