#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 dbg3(a, b, c) print(a); print(b); print(c) << "\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 sz(a) ((int)(a).size())
#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);
    vector <vector<int>> g(2 * n + 1);
    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];
        mp[{b[i], a[i]}] += c[i];
        g[b[i]].push_back(a[i]);
        g[a[i]].push_back(b[i]);
    }
    dbg(1);
    
    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) {
        int mx = 0, cur = mp[{a[i], b[i]}];
        int p = a[i], q = b[i];
        // if (sz(g[p]) > sz(g[q])) swap(p, q);
        for (auto c : g[p]) {
            if (c == q) continue;
            int cur = mp[{c, p}] + mp[{c, q}];
            chmax(mx, cur);
        }
        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 << "###########";
    }
} 