#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        vector<ll> a(n), b(n), c(n);
        vector<ll> popularity(2*n, 0);
        map<ll, vector<ll>> groups;
        map<pair<ll, ll>, ll> groups2;
        for (ll i = 0; i < n; i++) {
            cin >> a[i] >> b[i] >> c[i];
            a[i]--; b[i]--;
            popularity[a[i]] += c[i];
            popularity[b[i]] += c[i];
            groups[a[i]].push_back(i);
            groups[b[i]].push_back(i);
            groups2[{a[i], b[i]}] = i;
            groups2[{b[i], a[i]}] = i;
        }
        sort(popularity.begin(), popularity.end());
        ll ans = popularity[popularity.size()-1];
        for (auto it : groups) {
            for (ll i = 0; i < it.second.size(); i++) {
                for (ll j = i + 1; j < it.second.size(); j++) {
                    if (ans < c[it.second[i]] + c[it.second[j]] + c[groups2[{a[it.second[i]], b[it.second[j]]}]] - 1) {
                        ans = c[it.second[i]] + c[it.second[j]] + c[groups2[{a[it.second[i]], b[it.second[j]]}]] - 1;
                    }
                }
            }
        }
        cout << ans << '\n';
    }
}