#include <bits/stdc++.h>
#define  ll long long
using namespace std;

void solve() {
    //okay we have n types of mans and from 1 to 2*n num of woman
    //we need to find the biggest count of pairs that like 1 woman
    int n;
    cin >> n;
    int mxas = 0;
    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];
        mxas = max(mxas, v[i][2]);
    }
    int s = 0;
    int mx = 0;
    for(int i=0; i<n-1; i++) {
        int a = v[i][0];
        int b = v[i][1];
        int c = v[i][2];
        s = c;
        for(int j = i+1; j<n; j++) {
            if(a==v[j][1] || b==v[j][0]) {
                s+=v[j][2];
            }
        }
        mx = max(mx, s);
        s = 0;
    }
    if(n==2) {
        if(v[0][0]==v[1][1] || v[0][1]==v[1][0]) {
            cout << v[0][2]+v[1][2] << endl;
        } else {
            cout << max(v[0][2], v[1][2]) << endl;
        }
    } else {
        cout << max(mx, mxas) << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
}
