#include <iostream>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>


using namespace std;
int main() {

    int T;
    cin >> T;
    vector<int> finalResults;
    while (T--) {
        int n;
        cin >> n;
        vector<int> a(n, 0);
        vector<int> b(n, 0);
        vector<int> c(n, 0);
        for (int i = 0; i < n; i++) {
            cin >> a[i] >> b[i] >> c[i];
        }
        int max = -1;
        for (int i = 0; i < n; i++) {
            int mainA = a[i], mainB = b[i];
            vector<int> ta;
            vector<int> tb;
            vector<int> tc;
            for (int j = 0; j < n; j++) {
                if (a[j] == mainA || a[j] == mainB || b[j] == mainA || b[j] == mainB) {
                    if (j != i) {
                        ta.push_back(a[j]);
                        tb.push_back(b[j]);
                        tc.push_back(c[j]);
                    }
                }
            }
            vector<int> typesGirls;
            for (int j = 0; j < ta.size(); j++) {
                typesGirls.push_back(ta[j]);
                typesGirls.push_back(tb[j]);
            }
            for (int num : typesGirls) {
                int tempMax = 0;
                for (int j = 0; j < tc.size(); j++) {
                    if (num == ta[j] || num == tb[j]) {
                        tempMax += tc[j];
                    }
                }
                if (tempMax + c[i] > max) {
                    max = tempMax + c[i];
                }
            }
            if (max == -1) {
                for (int j = 0; j < c.size(); j++) {
                    if (c[j] > max)
                        max = c[j];
                }
            }

        }
        finalResults.push_back(max);
    }

    for (int i = 0; i < finalResults.size(); i++) {
        cout << finalResults[i] << endl;
    }
    return 0;
}
