#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), g1(2 * n);
    vector<tuple<int, int, int>> e;
    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));
        g1[a].push_back({b, c});
        g1[b].push_back({a, c});
        e.push_back({a, b, c});
    }

    int ans = *max_element(all(h));
    for (int v = 0; v < 2 * n; v++) {
        sort(rall(g[v]));
        sort(all(g1[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;
                auto it = lower_bound(all(g1[u]), make_pair(x, 0ll));
                if (it != g1[u].end() && it->first == x) {
                    ans = max(ans, g[v][i].first + w + it->second);
                }
                i++;
                cnt_i++;
            }
            else {
                int x = g[u][j].second;
                auto it = lower_bound(all(g1[v]), make_pair(x, 0ll));
                if (it != g1[v].end() && it->first == x) {
                    ans = max(ans, g[u][j].first + w + it->second);
                }
                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();
    }
}