#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;
};

bool cmp(A a,A b) {
    return a.c > b.c;
}

int main()
{
    fastread;
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll res = 0;
        vector<A> v(n);
        map<pll,ll> mp;
        map<ll,ll> mp1;
        ll mx = 0,mxx = 0;
        for (ll i = 0; i < n; i++) {
            cin >> v[i].a >> v[i].b >> v[i].c;
            mp[{v[i].a,v[i].b}] = v[i].c;
            mp1[v[i].a] += v[i].c;
            mp1[v[i].b] += v[i].c;
            mx = max({mx,mp1[v[i].a],mp1[v[i].b]});
        }
        ll k = n;
        if(n * n > 10000000)
            k = sqrt(n);
        sort(all(v),cmp);
        for (ll i = 0; i < k - 1; i++) {
            for (ll j = i + 1; j < k; j++) {
                if (v[i].a != v[j].a && v[i].a != v[j].b && v[i].b != v[j].a && v[i].b != v[j].b) {
                    ll zm = max(mp[{v[i].a,v[i].b}],mp[{v[j].a,v[j].b}]);
                    res = max(res,zm);
                }
                if (v[i].a == v[j].a) {
                    pll x = {v[i].b,v[j].b};
                    pll y = {v[i].a,v[i].b};
                    pll z = {v[j].a,v[j].b};
                    if (v[i].b > v[j].b)
                        x = {v[j].b,v[i].b};
                    ll zm = mp[x] + mp[y] + mp[z];
                    res = max(res, zm);
                }
                else if (v[i].a == v[j].b) {
                    pll x = {v[i].b,v[j].a};
                    pll y = {v[i].a,v[i].b};
                    pll z = {v[j].a,v[j].b};
                    if (v[i].b > v[j].a)
                        x = {v[j].a,v[i].b};
                    ll zm = mp[x] + mp[y] + mp[z];
                    res = max(res, zm);
                }
                else if (v[i].b == v[j].a) {
                    pll x = {v[i].a,v[j].b};
                    pll y = {v[i].a,v[i].b};
                    pll z = {v[j].a,v[j].b};
                    if (v[i].a > v[j].b)
                        x = {v[j].b,v[i].a};
                    ll zm = mp[x] + mp[y] + mp[z];
                    res = max(res, zm);
                }
                else if (v[i].b == v[j].b) {
                    pll x = {v[i].a,v[j].a};
                    pll y = {v[i].a,v[i].b};
                    pll z = {v[j].a,v[j].b};
                    if (v[i].a > v[j].a)
                        x = {v[j].a,v[i].a};
                    ll zm = mp[x] + mp[y] + mp[z];
                    res = max(res, zm);
                }
            }
        }
        cout << max(res,mx) << '\n';
    }

    return 0;
}

