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

#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define all(x) x.begin(), x.end()
#define fastread ios_base::sync_with_stdio(0);cin.tie(NULL)
#define sum(x) accumulate(all(x),0LL)
#define what_is(x) cerr << #x << " = " << (x) << '\n'
#define pb push_back

struct A {
    int a,b,c;
};

int main()
{
    fastread;
    int t;
    cin >> t;
    while (t--) {
        int n;
        ll ans = 0;
        cin >> n;
        vector<A> v(n);
        pll mx = {0,0};
        map<int,int> mp;
        vii g[2*n+5];
        map<pii,int> mg;
        for (int i=0;i<n;++i) {
            cin >> v[i].a >> v[i].b >> v[i].c;
            mg[{v[i].a,v[i].b}] = v[i].c;
            mp[v[i].a] += v[i].c;
            mp[v[i].b] += v[i].c;
            if (mp[v[i].a] > mx.second) {
                mx.second = mp[v[i].a];
                mx.first = v[i].a;
            }
            if (mp[v[i].b] > mx.second) {
                mx.second = mp[v[i].b];
                mx.first = v[i].b;
            }
            g[v[i].a].pb(v[i].b);
            g[v[i].b].pb(v[i].a);
        }
        ans = mx.second;
        for (int i=0;i<g[mx.first].size()-1;++i) {
            for (int j=i+1;j<g[mx.first].size();++j) {
                ll zm = mg[{g[mx.first][i],g[mx.first][j]}] + mg[{g[mx.first][j],g[mx.first][i]}] + mg[{mx.first,g[mx.first][j]}] + mg[{mx.first,g[mx.first][i]}];
                ans = max(zm,ans);
            }
        }
        cout << ans << '\n';
    }

    return 0;
}

