#include <bits/stdc++.h>
using namespace std;

#define int long long
#define len(v) (int) (v.size())
#define all(v) v.begin(), v.end()
#define rall(v) rbegin(v), rend(v)

istream& operator>>(istream& is, vector<int>& v) {
    for (int i = 0; i < len(v); i++) {
        is >> v[i];
    }
    return is;
}

ostream& operator<<(ostream& os, vector<int>& v) {
    for (int i = 0; i < len(v); i++) {
        os << v[i] << ' ';
    }
    return os;
}

void solve() {
    int n;
    cin >> n;
    vector<int> h(2 * n, 0);
    vector<vector<pair<int, int>>> g(2 * n);
    vector<tuple<int, int, int>> e;
    map<pair<int, int>, int> mp;
    for (int i = 0; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        h[a] += c;
        h[b] += c;
        g[a].push_back(make_pair(c, b));
        g[b].push_back(make_pair(c, a));
        e.push_back({a, b, c});
        mp[{a, b}] = c;
    }

    int ans = *max_element(all(h));
    for (int v = 0; v < 2 * n; v++) {
        sort(rall(g[v]));
    }
    for (auto [v, u, w] : e) {
        int i = 0, j = 0, cnt_i = 0, cnt_j = 0;
        while (cnt_i < 2 && cnt_j < 2 && i < len(g[v]) && j < len(g[u])) {
            if (g[v][i].second == u) {
                i++;
                continue;
            }
            if (g[u][j].second == v) {
                j++;
                continue;
            }
            if (g[v][i].first >= g[u][j].first) {
                int x = g[v][i].second;
                if (mp.count({min(x, u), max(x, u)})) {
                    ans = max(ans, g[v][i].first + w + mp[{min(x, u), max(x, u)}]);
                }
                i++;
                cnt_i++;
            }
            else {
                int x = g[u][j].second;
                if (mp.count({min(x, v), max(x, v)})) {
                    ans = max(ans, g[u][j].first + w + mp[{min(x, v), max(x, v)}]);
                }
                j++;
                cnt_j++;
            }
        }
    }

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

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

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