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

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i] >> b[i] >> c[i];
    }
    ll mx = 0, cost = 0;
    vector<pair<int, int>> p;
    for(int i=0; i<n; i++) {
        int a1 = a[i], b1 = b[i], c1 = c[i];
        cost+=c1;
        p.push_back({a1, b1});
        for(int j=0; j<n; j++) {
            if(i!=j) {
                int a2 = a[j], b2 = b[j], c2 = c[j];
                if(p.size()==1) {
                    if(a2==a1 || a2==b1 || b2==a1 || b2==b1) {
                        p.push_back({a2, b2});
                        cost+=c2;
                    }
                } else {
                    bool ok = true;
                    for(int k=0; k<p.size(); k++) {
                        int aa = p[k].first, bb = p[k].second;
                        if(aa!=a2 && aa!=b2 && bb!=b2 && bb!=a2) {
                            ok = false;
                            break;
                        }
                    }
                    if(ok==true) {
                        cost+=c2;
                        p.push_back({a2, b2});
                    }
                }
            }
        }

        p.clear();
        mx = max(mx, cost);
        cost = 0;
    }
    cout << mx << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
}
