#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;

#define el '\n'
#define ff first
#define ss second
#define pii pair <int, int>
#define pb push_back
#define popb pop_back
#define fr(x, l, r) for(ll (x) = l; (x) <= r; (x)++)
#define frb(x, r, l) for(ll (x) = r; (x) >= l; (x)--)

mt19937_64 rnd(time(0));

ll const N = 7e5 + 100;
ll const K = 1;

void chmax(long long &a, long long b) {
    a = max(a, b);
}

ll n;

int a[N], b[N], mx[2 * N];
long long c[N], mp[2 * N];
vector <pii> g[2 * N];

void solve() {
    cin >> n;

    map <pii, ll> cnt;
    long long ans = 0;
    fr(i, 1, n) {
        cin >> a[i] >> b[i] >> c[i];
        mp[a[i]] += c[i];
        mp[b[i]] += c[i];
        chmax(ans, mp[a[i]]);
        chmax(ans, mp[b[i]]);
        cnt[{a[i], b[i]}] = c[i];
        cnt[{b[i], a[i]}] = c[i];
    }

    fr(i, 1, n) {
        if(c[i] > cnt[{a[i], mx[a[i]]}])
            mx[a[i]] = b[i];
        if(c[i] > cnt[{b[i], mx[b[i]]}])
            mx[b[i]] = a[i];
    }

    fr(i, 1, n) {
        ll A = a[i], B = b[i];

        ll C = mx[A];
        if(C != B) {
            long long res = c[i] + cnt[{C, A}] + cnt[{C, B}];
            chmax(ans, res);
        }

        C = mx[B];
        if(C != A) {
            long long res = c[i] + cnt[{C, A}] + cnt[{C, B}];
            chmax(ans, res);
        }
    }
    cout << ans << el;

    fr(i, 1, n + n) {
        mp[i] = 0;
        g[i].clear();
        mx[i] = 0;
    }
}

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

    int t = 1;
    cin >> t;
    while(t--)
        solve();
}