#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    ll t;
    cin >> t;

    while(t--){
        ll n;
        cin >> n;

        vector<vector<ll>> a(n, vector<ll> (3));

        for(auto &i: a)
            for(auto &j: i)
                cin >> j;

        ll ans = 0;

        vector<ll> tot(2 * n + 1);
        for(auto i: a)
            tot[i[0]] += i[2], tot[i[1]] += i[2];

        map<pair<ll, ll>, ll> mp;

        for(auto i: a)
            mp[{i[0], i[1]}] += i[2];

        for(auto i: a){
            ans = max({ans, tot[i[0]], tot[i[1]]});

            for(ll x = 1; x <= 2 * n; x++){
                if(x == i[0] || x == i[1])
                    continue;

                ll cnt = mp[{min(i[0], x), max(i[0], x)}] + mp[{min(i[1], x), max(i[1], x)}];
                ans = max(ans, cnt + i[2]);
            }
        }

        cout << ans << endl;
    }

    return 0;
}

/*
1
5
1 2 1
1 3 4
4 5 2
3 4 2
1 4 3

4
1 2 3
2 3 4
3 5 4
1 3 2

*/
