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

long long n, ans;
vector<long long> A, B, C;
vector<long long> v1;

long long check1(){
    v1.clear();
    v1.resize(2*n+5);
    for (int i = 0; i < n; i++){
        v1[A[i]] += C[i];
        v1[B[i]] += C[i];
    }
    long long res = 0;
    for (int i = 1; i <= 2*n; i++)
        res =  max(res, v1[i]);
    return res;
}

bool check(int i, int j){
    return (A[i] == A[j] || B[i] == A[j] || A[i] == B[j] || B[i] == B[j]);
}

long long check2(){
    long long res = 0;
    for (int i = 0; i < n-2; i++)
        for (int j = i+1; j < n-1; j++)
            for (int k = j+1; k < n; k++)
                if (check(i, j) && check(i, k) && check(j, k))
                    res = max(res, C[i]+C[j]+C[k]);
    return res;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    long long T = 1;
    cin >> T;
    for (int i = 0; i < T; i++){
        ans = 0;
        A.clear();
        B.clear();
        C.clear();


        cin >> n;
        A.resize(n+5);
        B.resize(n+5);
        C.resize(n+5);

        for (int j = 0; j < n; j++)
            cin >> A[j] >> B[j] >> C[j];
        ans = max(check1(), check2());
        cout << ans << endl;
    }
    return 0;
}




