#include <bits/stdc++.h>

using namespace std;
#define int long long
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORd(i, a, b) for(int i = b-1; i>=a; --i)
#define fr first
#define sd second
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define pr(s) cout << s << endl
#define pr2(s, q) cout << s << ' ' << q << endl
#define pr3(s, q, r) cout << s << ' ' << q << ' ' << r << endl
#define fl(s) cout << s << ' '
#define fl2(s, q) cout << s << ' ' << q << ' '
#define fl3(s, q, r) cout << s << ' ' << q << ' ' << r << ' '
#define endl '\n'
#define all(A) A.begin(), A.end()
#define rall(A) A.begin(), A.end()
#define bub(A) sort(all(A))
#define rbub(A) sort(rall(A))
#define pb push_back
#define ins insert
#define ppb pop_back
#define INF 10000001ll
const int nmax = 700007;
int n, a[nmax], b[nmax], c[nmax], res, I, ans[2*nmax], mx;
vpii g[2*nmax];
map<int, int> mp;
void dfs(int v, int par){
    for(auto to: g[v]){
        int tmp = mp[INF * par + to.fr];
        if(tmp > 0){
            res = max(res, c[I] + c[to.sd] + tmp);
        }
    }
}
void solve(){
    cin >> n;
    res = 0;
    mp.clear();
    FOR(i, 1, 2*n+1){
        g[i].clear();
        ans[i] = 0;
    }
    FOR(i, 1, n+1){
        cin >> a[i] >> b[i] >> c[i];
        mx = max(mx, c[i]);
        mp[INF * a[i] + b[i]] = c[i];
        g[a[i]].pb({b[i], i});
        ans[a[i]] += c[i];
        ans[b[i]] += c[i];
    }
    FOR(i, 1, 2*n + 1) res = max(ans[i], res);
    if(res >= 2ll*mx){
        pr(res); return;
    }
    FOR(i, 1, n + 1){
        I = i;
        dfs(b[i], a[i]);
    }
    pr(res);
}

main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ttt = 1;
    cin >> ttt;
   // freopen("input.txt" , "r", stdin);

    while(ttt--) {solve();}
    return 0;
}
