#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <array>
#define ll long long
#define pii std::pair<int, int>
void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n), b(n), c(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i] >> b[i] >> c[i];
    }
    auto is_connected = [&](const int& x, const int& y) -> bool {
        return (a[x] == b[y] || a[y] == b[x] || a[x] == a[y] || b[x] == b[y]);
    };
    int ans = 0;
    for (int mask = 0; mask < (1 << n); ++mask) {
        std::vector<int> v;
        int cur = 0;
        for (int i = 0; i < n; ++i) {
            if (mask & (1 << i)) {
                v.push_back(i);
                cur += c[i];
            }
        }
        bool flag = true;
        for (int& i : v) {
            for (int& j : v) {
                if (!is_connected(i, j)) {
                    flag = false;
                    break;
                }
            }
        }
        if (flag) ans = std::max(ans, cur);
    }
    std::cout << ans << '\n';
}
int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}