#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);
        map<ll, ll> c;
        for (int i = 0; i < n; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            a[i] = {{x, y}, z};
            c[x] += z; c[y] += z;
        }
        ll res = 1;
        for (auto [_, x] : c) res = max(res, x);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    if (i == j || j == k || i == k) continue;
                    if (!(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)) continue;
                    if (!(a[k].first.first == a[j].first.first || a[k].first.second == a[j].first.first || a[k].first.first == a[j].first.second || a[k].first.second == a[j].first.second)) continue;
                    if (!(a[k].first.first == a[i].first.first || a[k].first.second == a[i].first.first || a[k].first.first == a[i].first.second || a[k].first.second == a[i].first.second)) continue;
                    res = max(res, a[i].second + a[j].second + a[k].second);
                }
            }
        }
        cout << res << '\n';
    }
}