#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;
        cin >> n;
        ll res = 0;
        vector<A> v(n);
        map<pii,int> mp;
        map<int,int> mp1;
        ll mx = 0;
        for (int 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,(ll)mp1[v[i].a],(ll)mp1[v[i].b]});
        }
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; 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);
                    continue;
                }
                if (v[i].a == v[j].a) {
                    ll zm = mp[{v[i].b,v[j].b}] + mp[{v[i].a,v[i].b}] + mp[{v[j].a,v[j].b}];
                    res = max(res, zm);
                }
                else if (v[i].a == v[j].b) {
                    ll zm = mp[{v[i].b,v[j].a}] + mp[{v[i].a,v[i].b}] + mp[{v[j].a,v[j].b}];
                    res = max(res, zm);
                }
                else if (v[i].b == v[j].a) {
                    ll zm = mp[{v[i].a,v[j].b}] + mp[{v[i].a,v[i].b}] + mp[{v[j].a,v[j].b}];
                    res = max(res, zm);
                }
                else if (v[i].b == v[j].b) {
                    ll zm = mp[{v[i].a,v[j].a}] + mp[{v[i].a,v[i].b}] + mp[{v[j].a,v[j].b}];
                    res = max(res, zm);
                }
            }
        }
        cout << max(res,mx) << '\n';
    }

    return 0;
}

