#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, a, b, c, x;
    cin >> t;
    unordered_map<ll, ll> mp;
    vector<pair<ll, ll>> v;
    while (t--) {
        cin >> n;
        v.clear();
        ll A[2*n+1][2*n+1];
        for (int i=1; i<2*n+1; i++) {
            for (int j=1; j<2*n+1; j++) {
                A[i][j] = 0;
            }
        }
        mp.clear();

        ll mx = -1;
        for (int i=0; i<n; i++) {
            cin >> a >> b >> c;
            v.pb(make_pair(a, b));
            A[a][b] = c;
            A[b][a] = c;
            //cout << A[a][b] << " " << A[b][a] << " " << c << "\n";
            mp[a]+=c;
            mp[b]+=c;
            mx = max(mx, max(mp[a], mp[b]));
        }
        for (int i=0; i<n; i++) {
            x = A[v[i].ff][v[i].ss];
            for (int j=1; j<2*n+1; j++) {
                if (j == v[i].ff || j == v[i].ss)
                    continue;
                mx = max(mx, (A[j][v[i].ff]+A[j][v[i].ss]+x));
                //cout << v[i].ff << " " << v[i].ss << " " << x << " " << j << endl;
            }
        }
        cout << mx << "\n";
    }
}