#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n;
    cin >> n;
    vector< pair<pair<int, int>, int> > vt;
    vector< int > v(2 * n + 1);
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        v[a] += c; v[b] += c;
        vt.emplace_back(make_pair(make_pair(a, b), c));
    }
    int ans = 0;
    for(auto& i : v) {
        ans = max(ans, i);
    }
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            for(int k = j + 1; k < n; ++k) {
                map<int, int> chk;
                chk[vt[i].first.first]++;
                chk[vt[i].first.second]++;
                chk[vt[j].first.first]++;
                chk[vt[j].first.second]++;
                chk[vt[k].first.first]++;
                chk[vt[k].first.second]++;
                bool f = true;
                for(auto& ll : chk) if(ll.second != 2) f = false;
                if(f) {
                    ans = max(ans, vt[i].second + vt[j].second + vt[k].second);
                }
            }
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    cin >> tt;
    while(tt--) {
        solve();
    }
}