#include <bits/stdc++.h>

using namespace std;

const int DIM = 7e5;
int q_boys[DIM], len;
map<int,long long> girls[2*DIM];
long long sum_girls[2*DIM];

long long solve(){
    long long max_gr = 0;
    cin >> len;
    for (int i = 0; i < 2*len; ++i){
        girls[i].clear();
        sum_girls[i] = 0;
    }
    for (int i = 0, a_i, b_i, c_i; i < len; ++i){
        cin >> a_i >> b_i >> c_i; --a_i; --b_i;

        if (girls[a_i].find(b_i) == girls[a_i].end())
            girls[a_i][b_i] = c_i;
        else
            girls[a_i][b_i] += c_i;
        if (girls[b_i].find(a_i) == girls[b_i].end())
            girls[b_i][a_i] = c_i;
        else
            girls[b_i][a_i] += c_i;
        sum_girls[a_i] += c_i;
        sum_girls[b_i] += c_i;
    }

    for (int i = 0; i < 2*len; ++i)
        max_gr = max(max_gr, sum_girls[i]);
    for (int a_i = 0; a_i < 2*len; ++a_i)
        for (auto [b_i, cost_1] : girls[a_i]){
            max_gr = max(girls[a_i][b_i], max_gr);
            for (auto [key, cost_2] : girls[b_i])
                if (girls[a_i].find(key) != girls[a_i].end())
                    max_gr = max(max_gr, cost_1 + cost_2 + girls[a_i][key]);
        }

    return max_gr;
}

void output(){
    for (int i = 0; i < 2*len; ++i){
        for (auto [key,el] : girls[i])
            cout << key+1 << '-' << el << ' ';
        cout << '\n';
    }
}

int main(){
    int q_test; cin >> q_test;

    for (int i = 0; i < q_test; ++i){
        cout << solve() << '\n';
        //output();
    }

	return 0;
}
