#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;
    for (int i=0; i<n; i++) {
        int a = v[i][0], b = v[i][1], c = v[i][2];
        cost = c;
        for (int j=i+1; j<n; j++) {
            int a1 = v[j][0], b1 = v[j][1], c1 = v[j][2];
            if (a1==a || b1==b || a1==b || b1==a) {
                cost+=c1;
            }
        }
        mx = max(mx, cost);
    }
    cout << mx << endl;
}

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