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

bool check (long a, long b, long c, long d, long e, long f) {
    vector <long> k;
    k.push_back(a);
    k.push_back(b);
    k.push_back(c);
    k.push_back(d);
    k.push_back(e);
    k.push_back(f);
    sort(k.begin(), k.end());
    if (k[0] == k[1] && k[2] == k[3] && k[4] == k[5])
    return true;
    return false;
}

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;
        long long res = 0;
        long maxc = 0;
        vector <long long> v(2*n+1,0);
        vector <long long> a(n),b(n),c(n);
        long long 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();
      //  cout << "MAX:" << maxn << endl;
        for (int j = 0; j < n; j++) {
            if (a[j] == maxn || b[j] == maxn)
            res+=c[j];
        }*/
        res = *max_element(v.begin(), v.end());
        for (int x = 0; x < n; x++)
        for (int y = 0; y < n; y++)
        for (int z = 0; z < n; z++) {
        if (check(a[x], b[x], a[y], b[y], a[z], b[z]))
        sum = max(sum, c[x]+c[y]+c[z]);
        }
        cout << max(res,sum) << endl;
       /* v.clear();
        a.clear();
        b.clear();
        c.clear();*/
    }
}
