#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<set<int>> bb;
    map<int, int> aa;
    for(int i = 0; i < n; i++){
        int a, b, c; cin >> a >> b >> c;
        set<int> tmp = {a, b};
        aa[i] = c;
        bb.push_back(tmp);
    }
    int mx = 0;
    for(int i = 0; i < n; i++){
        int ans = aa[i];
        for(int j = i+1; j < n; j++){

            for(auto x: bb[j]){
                if(bb[i].find(x) != bb[i].end()){
                    ans += aa[j];
                   // break;
                }
            }
        }
        mx = max(mx, ans);
    }
    cout << mx << endl;
}

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