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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        long n;
        cin >> n;
        int res = 0;
        vector <int> v(2*n+1,0);
        vector <int> a(n),b(n),c(n);
        int sum = 0;
        for (int j = 0; j < n; j++) {
            cin >> a[j] >> b[j] >> c[j];
            v[a[j]]+=c[j];
            v[b[j]]+=c[j];
        }
        int maxn = max_element(v.begin(), v.end()) - v.begin();
        for (int j = 0; j < n; j++) {
            if (a[j] == maxn || b[j] == maxn)
            res+=c[j];
        }
        for (int x = 0; x < n; x++)
        for (int y = x+1; y < n; y++)
        for (int z = y+1; z < n; z++) {
        if (a[x] == a[y] && b[x] == a[z] && b[y] == b[z])
        sum = max(sum, c[x] + c[y] + c[z]);
        }
        cout << sum << endl;
        v.clear();
        a.clear();
        b.clear();
        c.clear();
    }
}
