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


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll T;
    cin >> T;
    while(T--){
        ll n;
        cin >> n;
        ll a[n], b[n], c[n];
        for (ll i=0; i<n; i++){
            cin >> a[i] >> b[i] >> c[i];
        }
        vector<ll> sum(2*n+1,0);
        for(ll i=0; i<n; i++){
            sum[a[i]]+=c[i];
            sum[b[i]]+=c[i];
        }
        ll sum1 = *max_element(sum.begin(), sum.end());
        ll sum2 = 0;

        ll sum3 = 0;
        for(int i=0; i<n; i++){
            for (int j=0; j<n; j++){
                if (i==j) continue;
                if (a[i]==a[j] || a[i]==b[j] || b[i]==b[j]){
                    sum3=max(sum3, (c[i]+c[j]));
                }
            }
        }
        cout << max(sum1,max(sum2, sum3)) << endl;
    }
}