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

void solve() {
    int n;
    cin >> n;
    vector <array <long long, 3> > v(n);

    map <long long, long long> m;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> v[i][0] >> v[i][1] >> v[i][2];

        m[v[i][0]] += v[i][2];
        m[v[i][1]] += v[i][2];

        ans = max(ans, m[v[i][0]]);
        ans = max(ans, m[v[i][1]]);
    }

    for (int a = 0; a < n-2; a++) {
        for (int b = a+1; b < n-1; b++) {
            for (int c = b+1; c < n; c++) {
                for (int k1 = 0; k1 < 2; k1++) {
                    for (int k2 = 0; k2 < 2; k2++) {
                        for (int k3 = 0; k3 < 2; k3++) {
                            if (v[a][k1] == v[b][k2] && v[a][1-k1] == v[c][k3] && v[b][1-k2] == v[c][1-k3]) ans = max(ans, v[a][2] + v[b][2] + v[c][2]);
                        }
                    }
                }
            }
        }
    }
    cout << ans << "\n";
}

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

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