#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10, M = 2 * N,INF = 0x7f7f7f7f;
int tot;
int head[N],deg[N],d[N],f[N];
struct Edge{
int to,nxt,w;
}e[M];
void add(int u,int v,int w)
{
e[tot].to = v, e[tot].w = w, e[tot].nxt = head[u];
head[u] = tot++;
}
void dfs_d(int u,int fa)
{
if(deg[u] == 1)
{
d[u] = INF;
return;
}
for(int i = head[u]; ~i; i = e[i].nxt)
{
int to = e[i].to;
if(to == fa) continue;
dfs_d(to,u);
d[u] += min(e[i].w,d[to]);
}
}
void dfs_f(int u,int fa)
{
for(int i = head[u]; ~i; i = e[i].nxt)
{
int to = e[i].to;
if(to == fa) continue;
if(deg[to] == 1) f[to] = min(e[i].w,f[u]-e[i].w);
else
{
f[to] = d[to] + min(e[i].w,f[u]-min(e[i].w,d[to]));
dfs_f(to,u);
}
}
}
int main()
{
int T,n; cin >> T;
while(T--)
{
memset(deg,0,sizeof(deg));
memset(head, -1, sizeof head); tot = 0;
cin >> n;
for(int i = 1,a,b,c; i < n; ++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c); add(b,a,c);
deg[a]++,deg[b]++;
}
dfs_d(1,-1);
dfs_f(1,-1);
int res = 0;
for(int i = 1; i <= n; ++i) res = max(res,f[i]);
cout << res << endl;
}
return 0;
}