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

void solve() {
    int n;
    cin >> n;
    vector <array <long long, 2> > v[n*2+6];

    map <long long, long long> m;
    long long ans = 0, a, b, c;
    for (int i = 0; i < n; i++) {
        cin >> a >> b >> c;

        v[a].push_back({b, c});
        v[b].push_back({a, c});

        m[a] += c;
        m[b] += c;

        ans = max(ans, m[a]);
        ans = max(ans, m[b]);
    }
    int y, z;
    for (int x = 0; x <= 2*n + 5; x++) {
        for (int i = 0; i < v[x].size(); i++) {
            y = v[x][i][0];
            for (int j = 0; j < v[y].size(); j++) {
                z = v[y][j][0];
                for (int k = 0; k < v[z].size(); k++) {
                    a = v[z][k][0];

                    if (x == a) {
                        ans = max(ans, v[x][i][1] + v[y][j][1] + v[z][k][1]);
                    }
                }
            }
        }
    }

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

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

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