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

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