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

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> v(n, vector<int> (3));
    for(int i=0; i<n; i++) {
        cin >> v[i][0] >> v[i][1] >> v[i][2];
    }
    sort(v.begin(), v.end());
    vector<pair<int, int>> p;
    ll cost = 0, mx = 0;
    for(int i=0; i<n; i++) {
        cost=v[i][2];
        p.push_back({v[i][0], v[i][1]});
        for(int j=i+1; j<n; j++) {
            int a = v[j][0], b = v[j][1];
            int pa = v[j-1][0], pb = v[j-1][1];
            if(a==pa || a==pb || b==pa || b==pb) {
                if(p.size()==1) {
                    p.push_back({a, b});
                    cost+=v[j][2];
                } else {
                    bool ok = true;
                    for(int k=0; k<p.size(); k++) {
                        int pa1 = p[k].first, pb1 = p[k].second;
                        if(a!=pa1 && a!=pb1 && b!=pa1 && b!=pb1) {
                            ok = false;
                            break;
                        }
                    }
                    if(ok==true) {
                        cost+=v[j][2];
                        p.push_back({a, b});
                    } else {
                        /*
                        cout << i+1 << ":";
                        for(int jj = 0; jj<p.size(); jj++) {
                            cout << p[jj].first << " " << p[jj].second << " ";
                        }
                        */
                        p.clear();
                        break;
                    }
                }
            }
        }
        //cout << cost << endl;
        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();
    }
}
