#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("Unroll-loops")
#pragma GCC target("avx,avx2,tune=native")
using namespace std;

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

    long long m[1400006] = {0};
    long long ans = 0, a, b, c;
    long long b1 = -1e18, b2 = -1e18, b3 = -1e18;
    for (int i = 0; i < n; i++) {
        cin >> a >> b >> c;
        
        if (c > b1) {
            b3 = b2;
            b2 = b1;
            b1 = c;
        } else if (c > b2) {
            b3 = b2;
            b2 = c;
        } else if (c > b3) {
            b3 = 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]);
    }

    if (n < 3) {
        cout << ans << "\n";
        return;
    }
    if (b1 + b2 + b3 <= ans) {
        cout << ans << "\n";
        return;
    }

    int y, z;
    for (int x = 0; x <= 2*n + 1; 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]);
                        break;
                    }
                }
            }
        }
        if (ans == b1 + b2 + b3) break;
    }

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

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

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