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

void solve() {
    int n;
    cin>>n;
    vector<ll> a(n), b(n), c(n);
    vector<map<ll, ll>> grp(2 * n);
    for (int i = 0; i < n;i++) {
        cin>>a[i]>>b[i]>>c[i]; a[i]--; b[i]--;
        grp[a[i]][b[i]] += c[i];
        grp[b[i]][a[i]] += c[i];
    }
    ll res = 0;
    vector<ll> best(2 * n, -1e18);
    int ind = 0;
    for (auto& arr : grp) {
        ll cr = 0;
        for (int j = 0; j < ind - 1; j++) {
            for (auto& [k, v] : arr) {
                // cout << grp[j][ind] + v + grp[j][k] << " " << j << " " << ind << endl;
                res = max(res, grp[j][ind] + v + grp[j][k]);
            }
        }
        for (auto& [k, v] : arr) {
            cr += v;
            // cout << k << " " << best[ind] + v + best[k] << " supertest" << endl;
            // res = max(res, best[ind] + v + best[k]);
            // best[k] = max(best[k], v);
            // cout << k << " " << v << endl;
        }
        ind++;
        res = max(res, cr);
        // for (int i = 0; i < n;i++) {
        //     if (arr.find(a[i]) != arr.end() && arr.find(b[i]) != arr.end()) {
        //         res = max(res, arr[a[i]] + arr[b[i]] + c[i]);
        //     }
        // }
    }
    cout << res << endl;
}

int main() {
    // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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