#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define all(a) (a).begin(), (a).end()

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

    int tc;
    cin >> tc;
    while (tc--) {
        int n;
        cin >> n;
        vector<pair<pair<int, int>, ll>> a(n);
        for (int i = 0; i < n; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            a[i] = {{x, y}, z};
        }
        ll res = 1;
        for (int m = 0; m < (1 << n); m++) {
            bool ans = 1;
            ll s = 0;
            for (int i = 0; i < n; i++) {
                if (!((m >> i) & 1)) continue;
                for (int j = 0; j < n; j++) {
                    if (!((m >> j) & 1)) continue;
                    ans &= a[i].first.first == a[j].first.first || a[i].first.second == a[j].first.first || a[i].first.first == a[j].first.second || a[i].first.second == a[j].first.second;
                }
                s += a[i].second;
            }
            if (ans) res = max(res, s);
        }
        cout << res << '\n';
    }
}