#include <iostream>
#include <vector>
#include <math.h>
#define LLI long long int

using namespace std;

vector<vector<LLI>> types;
vector<LLI> ask;
LLI maximize;

void SecondStage(LLI a, LLI b, LLI c) {
    for (LLI i = 0; i < types.size(); i++) {
        if ((a == types[i][0] && b == types[i][1]) || (b == types[i][0] && a == types[i][1])) {
            maximize = max(maximize, c + types[i][2]);
            return;
        }
    }
    maximize = max(maximize, c);
}

void FirstStage(LLI a, LLI b, LLI c) {
    ask = { a, b, c };
    for (LLI i = 0; i < types.size(); i++) {
        if (ask == types[i]) continue;
        if (a == types[i][0]) SecondStage(b, types[i][1], c + types[i][2]);
        if (b == types[i][1]) SecondStage(a, types[i][0], c + types[i][2]);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    LLI n, t, i, j, a, b, c;
    vector<LLI> v;
    cin >> t;
    for (i = 0; i < t; i++) {
        cin >> n;
        maximize = 0;
        v = {};
        types = {};
        for (j = 0; j < n * 2; j++) {
            v.push_back(0);
        }
        for (j = 0; j < n; j++) {
            cin >> a >> b >> c;
            a--, b--;
            v[a] += c;
            v[b] += c;
            types.push_back({a, b, c});
            maximize = max(maximize, v[a]);
            maximize = max(maximize, v[b]);
        }
        for (j = 0; j < n; j++) {
            FirstStage(types[j][0], types[j][1], types[j][2]);
        }
        cout << maximize << endl;
    }
}
