#include <bits/stdc++.h>
#define int long long
#define elif else if
#define pb push_back
#define vt vector

using namespace std;

/*
int fnd(int x){
    while(link[x] != x){
        x = link[x];
    }
    return x;
}

void unite(int a, int b){
    if(sz[a] < sz[b]) swap(a, b);
    sz[a] += sz[b];
    link[b] = link[a];
}*/

void solve(){
    int n; cin >> n;

    vector<pair<pair<int, int>, int>> bb;
    map<int, int> aa;
    for(int i = 0; i < n; i++){
        int a, b, c; cin >> a >> b >> c;
        bb.push_back({{a, b}, c});
    }
    int mx = 0;
    for(int i = 0; i < n; i++){
        int tmp = bb[i].second;
        int x1 = bb[i].first.first;
        int x2 = bb[i].first.second;

        for(int j = i+1; j < n; j++){
            int y1 = bb[j].first.first;
            int y2 = bb[j].first.second;

            if(x1 == y1 || x2 == y1 || x2 == y1 || x2 == y2){
                tmp += bb[j].second;
            }
        }
        mx = max(mx, tmp);
    }
    cout << mx << endl;
}

signed main()
{
    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}
