#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ff first
#define ss second
#define pb push_back

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

    ll t, n, c, x;
    int a, b;
    cin >> t;
    unordered_map<ll, ll> mp;
    vector<unordered_set<ll>> v;
    map<pair<int, int>, int> qq;
    vector<pair<int, int>> e;
    while (t--) {
        cin >> n;
        v.clear();
        v.resize(2*n+1);
        mp.clear();
        qq.clear();
        e.clear();

        ll mx = -1;
        for (int i=0; i<n; i++) {
            cin >> a >> b >> c;
            e.pb(make_pair(a, b));
            v[a].insert(b);
            v[b].insert(a);
            qq[make_pair(a, b)] = c;
            qq[make_pair(b, a)] = c;
            mp[a]+=c;
            mp[b]+=c;
            mx = max(mx, max(mp[a], mp[b]));
        }
        for (int i=0; i<n; i++) {
            x = qq[make_pair(e[i].ss, e[i].ff)];
            if (v[e[i].ff].size() <= v[e[i].ss].size()) {
                for (auto u: v[e[i].ff]) {
                    //cout << u << "??\n";
                    if (v[e[i].ss].count(u)) {
                        //cout << e[i].ff << " " << e[i].ss << " " << u << " " << qq[make_pair(u, e[i].ff)]+qq[make_pair(u, e[i].ss)]+x << "???\n";
                        mx = max(mx, qq[make_pair(u, e[i].ff)]+qq[make_pair(u, e[i].ss)]+x);
                    }
                }
            }
        }
        cout << mx << "\n";
    }
}