#include <bits/stdc++.h>

using namespace std;

#define print(a) cerr << "[" << #a << " = " << a << "] "
#define dbg(a) print(a) << "\n"
#define dbg2(a, b) print(a); print(b) << "\n"

#define int long long
#define ll long long
#define pi pair<int, int>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define divup(a, b) (((a) + (b) - 1) / (b))

template <class T> bool chmax(T&a, const T&b) {return b > a ? a = b, 1 : 0;};
template <class T> bool chmin(T&a, const T&b) {return b < a ? a = b, 1 : 0;};

const int mod = 1e9 + 7;
const int inf = (int)(1e18) + 7;

const int maxn = 2e5 + 7;
const int N = 1e4 + 7;
const int logn = 20;

void solve() {
    int n;
    cin >> n;
    vector <int> a(n), b(n), c(n);
    vector <int> cnt(2 * n + 1, 0);
    map <pi, int> mp;
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i] >> c[i];
        cnt[a[i]] += c[i];
        cnt[b[i]] += c[i];
        mp[{a[i], b[i]}] += c[i];
    }
    
    int ans = 0;
    for (int i = 1; i <= 2 * n; ++i) {
        int cur = cnt[i];
        ans = max(ans, cnt[i]);
    }
    for (int i = 0; i < n; ++i) {
        map <pi, int> mp2;
        int cur = mp[{a[i], b[i]}];
        vector <int> cnt(2 * n + 1, 0);
        int mx = 0;
        for (int j = 0; j < n; ++j) {
            if (j == i) continue;
            if (a[j] == a[i]) {
                cnt[b[j]] += c[j];
                mx = max(mx, cnt[b[j]]);
            }
            if (b[j] == b[i]) {
                cnt[a[j]] += c[j];
                mx = max(mx, cnt[a[j]]);
            }
        }
        cur += mx;
        ans = max(ans, cur);
    }

    cout << ans << '\n';
}

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

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
        cerr << "###########";
    }
} 