#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define el '\n'
#define ff first
#define ss second
#define pb push_back
#define point pair <ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

const ll N = 7e5 + 10;
ll a[N], b[N], c[N], cnt[N + N];

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

    int tests;
    cin >> tests;

    while (tests--) {
        ll n;
        cin >> n;

        for (ll i = 1; i <= n * 2; i++)
            cnt[i] = 0;

        ll ans = 0;
        for (ll i = 1; i <= n; i++) {
            cin >> a[i] >> b[i] >> c[i];
            cnt[a[i]] += c[i];
            cnt[b[i]] += c[i];
        }

        for (ll i = 1; i <= n * 2; i++)
            ans = max(ans, cnt[i]);

        map <point, ll> cnt;
        for (ll i = 1; i <= n; i++)
            cnt[{a[i], b[i]}] = c[i];

        for (ll i = 1; i <= n; i++) {
            for (ll j = i + 1; j <= n; j++) {
                set <ll> cur;
                cur.insert(a[i]);
                cur.insert(b[i]);
                cur.insert(a[j]);
                cur.insert(b[j]);

                if (cur.size() == 4)
                    continue;

                vector <ll> now;
                for (ll x : cur)
                    now.pb(x);

                ans = max(ans, cnt[{now[0], now[1]}] + cnt[{now[1], now[2]}] + cnt[{now[0], now[2]}]);
            }
        }
        cout << ans << el;
    }

    return 0;
}
