#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;

    set<int> was1;
    set<int> was2;
    map<int, int> b;

    for(int i = 0; i < n; i++){
        int tmp = bb[i].second;
        int x1 = bb[i].first.first;
        int x2 = bb[i].first.second;

        b[x1]++;
        b[x2]++;

        map<int, int> g = b;
        int it = 1;
        for(int j = i+1; j < n; j++){
            int y1 = bb[j].first.first;
            int y2 = bb[j].first.second;

            if(g[y1] == it){
                g[y1]++;
                it++;
                tmp += bb[j].second;
            }
            if(g[y2] == it){
                it++;
                g[y2]++;
                tmp += bb[j].second;
            }
        }

        mx = max(mx, tmp);
    }
    cout << mx << endl;
}

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