/* I want linux on uoi */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ttree = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(X) (X).begin(), (X).end()

void solve() {
	int n;
	cin >> n;
	vector<vector<pair<int, int>>> g(n + n);
	long long r = 0;
	for (int i = 0; i < n; i ++) {
		int a, b, c;
		cin >> a >> b >> c;
		a --, b --;
		g[a].emplace_back(b, c);
		g[b].emplace_back(a, c);
	}
    vector<int> max_edge(n + n);
	for (int i = 0; i < 2 * n; i ++) {
		sort(g[i].begin(), g[i].end());
		long long t = 0;
        int ctr = 0;
        pair<int, int> pt = {-1, -1};
		for (auto [j, w] : g[i]) {
			t += w;
            pt = max(pt, make_pair(w, ctr ++));
		}
		r = max(r, t);
        max_edge[i] = pt.second;
	}
    constexpr int LIME = 1000;
	for (int i = 0; i < n + n; i ++) {
        if (g[i].empty()) continue;
        int u = g[i][max_edge[i]].first;
        for (auto [j, e] : g[i]) {
            if (u == j) continue;
            auto it = lower_bound(all(g[j]), make_pair(u, -1));
            if (it == g[j].end() || it->first != u) {
                continue;
            }
            r = max(r, g[i][max_edge[i]].second + (long long)e + it->second);
        }
	}
	cout << r << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int nt;
	cin >> nt;
	while (nt --)
		solve();
}
