#include<bits/stdc++.h>
using namespace std;
#define nL “\n”
#define pii pair<int, int>
class Graph {
int V;
list *l;
public:
Graph(int v)
{
V = v;
l = new list[V];
}
void addEdge(int u, int v, int cost)
{
l[u].pb({v, cost});
l[v].pb({u, cost});
}
int DFS(int u, bool *vis, int *count, int &ans)
{
vis[u] = true;
count[u] = 1;
for (auto nbr : l[u])
{
int v = nbr.first;
int wt = nbr.second;
if (!vis[v])
{
count[u] += DFS(v, vis, count, ans);
int nx = count[v];
int ny = V - nx;
ans += 2 * min(nx, ny) * wt;
}
}
return count[u];
}
int maxDist()
{
bool *vis = new bool[V];
int *count = new int[V];
for (int i = 0; i < V; i++)
{
vis[i] = false;
count[i] = 0;
}
int ans = 0;
DFS(0, vis, count, ans);
return ans;
}
};
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
Graph g(n);
for (int i = 0; i < n - 1; i++)
{
int x, y, z;
cin >> x >> y >> z;
g.addEdge(x, y, z);
}
cout << g.maxDist();
}
return 0;
}