#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());
    ll cost = 0, mx = 0;
    set<int> x;
    for(int i=0; i<n; i++) {
        int a = v[i][0], b = v[i][1], c = v[i][2];
        x.insert(a); x.insert(b);
        cost+=c;
        for(int j=i+1; j<n; j++) {
            if(i!=j) {
                bool ok = true;
                int a2 = v[j][0], b2 = v[j][1], c2 = v[j][2];
                if(x.size()==2) {
                    if(a2==a || a2==b || b2==a || b2==b) {
                        //cout << "+ " << j << " ";
                        x.insert(a2);
                        x.insert(b2);
                        cost+=c2;
                    }
                } else {
                ok = true;
                for(auto h : x) {
                    if(a2!=h && b2!=h) {
                        ok = false;
                        break;
                    }
                }
                if(ok) {
                    //cout << "+ " << j << " ";
                    cost+=c2;
                    x.insert(a2);
                    x.insert(b2);
                }
                }
            }
        }
        /*
        cout << i << " : ";
        for(auto y : x) {
            cout << y << " ";
        }
        cout << endl;
        cout << cost << endl;
        */
        mx = max(cost, mx);
        x.clear();
        cost = 0;
    }
    cout << mx << endl;
}


int main() {
    int test;
    cin >> test;
    while(test--) {
        solve();
    }
}
