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

void solve() {
    int n;
    cin >> n;
    vector <array <int, 2> > v[n*2+6];
    int h[2*n+1][2*n+1] = {0};

    long long m[2*n+1];
    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});
        h[a][b] = c;
        h[b][a] = c;

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

        ans = max(ans, m[a]);
        ans = max(ans, m[b]);
    }
    int y, z;
    for (int x = 1; x <= 2*n; 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];
                if (x == z) continue;
                if (h[z][x] != 0) ans = max(ans, v[x][i][1] + v[y][j][1] + h[z][x]);
            }
        }
    }

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

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

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