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

void solve() {
    int n;
    cin >> n;

    int v[n][3];
    int m[2*n+1] = {0};

    map <int, map <int, int> > d;
    int ans = 0;
    int a, b, c;
    for (int i = 0; i < n; i++) {
        cin >> a >> b >> c;
        if (a > b) swap(a, b);
        v[i][0] = a;
        v[i][1] = b;
        v[i][2] = c + d[a][b];
        d[a][b] += c;

        m[a] += c;
        m[b] += c;
        ans = max(ans, m[a]);
        ans = max(ans, m[b]);
    }

    for (int x = 0; x < n-2; x++) {
        for (int y = x+1; y < n-1; y++) {
            for (int z = y+1; z < n; z++) {
                for (int k1 = 0; k1 < 2; k1++) {
                    for (int k2 = 0; k2 < 2; k2++) {
                        for (int k3 = 0; k3 < 2; k3++) {
                            if (v[x][k1] == v[y][k2] && v[x][1-k1] == v[z][k3] && v[y][1-k2] == v[z][1-k3]) ans = max(ans, v[x][2] + v[y][2] + v[z][2]);
                        }
                    }
                }
            }
        }
    }

    cout << ans << "\n";
    return;
}

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

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