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

using ll= long long;

bool equals(ll a, ll b, ll a1, ll b1) {
    if (a==a1 || a==b1 || b==a1 || b==b1) return true;
    return false;
}

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

    ll t;
    cin>>t;
    for (ll i = 0;i< t;i++) {
        ll n;
        cin>>n;
        vector<vector<ll>> boys_types(n, vector<ll>(3,0));
        vector<pair<ll, vector<pair<ll, ll>>>> sums(n);
        for (ll j = 0;j < n;j++) {
            for (ll u = 0; u < 3; u++) {
                cin>> boys_types[j][u];
            }

        }
        for (ll j = 0;j < n;j++) {
            sums[j].second.push_back(make_pair(boys_types[j][0], boys_types[j][1]));
            sums[j].first = boys_types[j][2];

            for (ll u = j+1; u < n; u++) {
                if (equals(boys_types[j][0], boys_types[j][1], boys_types[u][0], boys_types[u][1])) {
                    sums[j].second.push_back(make_pair(boys_types[u][0], boys_types[u][1]));
                    sums[j].first += boys_types[u][2];
                }
            }
        }
        // for (ll j = 0;j < n;j++) {
        //     cout<<sums[j].first << " ";
        // }
        ll ans = 0;
        for (ll j = 0; j < n;j++) {
            bool is_good = true;
            for (ll u = 1; u < sums[j].second.size();u++) {

                for (ll k = u+1; k < sums[j].second.size();k++) {
                    if (!equals(sums[j].second[u].first, sums[j].second[u].second, sums[j].second[k].first, sums[j].second[k].second)) {
                        is_good = false;
                    }
                }
            }
            if (is_good) ans = max(ans, sums[j].first);
        }
        cout << ans << endl;
    }
    return 0;
}
