#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int NUMBER = 2e5 + 1, EDGE_NUMBER = NUMBER << 1;
const int INF = 0x3f3f3f3f;
int number;
int head[NUMBER], edge_end[EDGE_NUMBER], next_edge[EDGE_NUMBER], weights[EDGE_NUMBER], edge_index;
int dp1[NUMBER], dp2[NUMBER], deg[NUMBER];
void add_edge(int start, int end, int val) {
edge_end[edge_index] = end, next_edge[edge_index] = head[start], weights[edge_index] = val, head[start] = edge_index++;
}
int dfs1(int _node, int prev) {
if (deg[_node] == 1) {
dp1[_node] = INF;
return dp1[_node];
}
dp1[_node] = 0;
for (int i = head[_node]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == prev) continue;
dp1[_node] += min(weights[i], dfs1(ver, _node));
}
return dp1[_node];
}
void dfs2(int _node, int prev) {
for (int i = head[_node]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == prev) continue;
if (deg[ver] == 1) dp2[ver] = min(weights[i], dp2[_node] - weights[i]);
else {
dp2[ver] = dp1[ver] + min(dp2[_node] - min(dp1[_node], weights[i]), weights[i]);
dfs2(ver, _node);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int cases;
cin >> cases;
while (cases--) {
memset(head, -1, sizeof head);
memset(dp1, 0, sizeof dp1);
memset(dp2, 0, sizeof dp2);
memset(deg, 0, sizeof deg);
edge_index = 0;
cin >> number;
for (int i = 0; i < number - 1; ++i) {
int ver1, ver2, val;
cin >> ver1 >> ver2 >> val;
add_edge(ver1, ver2, val);
add_edge(ver2, ver1, val);
deg[ver1]++, deg[ver2]++;
}
int root = 0;
for (int i = 1; i <= number; ++i) {
if (deg[i] != 1) {
root = i;
break;
}
}
if (root == 0) {
cout << weights[0] << endl;
continue;
}
dfs1(root, -1);
dp2[root] = dp1[root];
dfs2(root, -1);
int res = -INF;
for (int i = 1; i <= number; ++i) res = max(res, dp2[i]);
cout << res << endl;
}
return 0;
}