#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=
5100000;
int m,n,r,pre[maxn],sum;
struct node
{
int u,v,w;
}edge[maxn];
int cmp(node aa,node bb)
{
return aa.w<bb.w;
}
int findd(
int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
int i=x,j;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
int zyz()
{
int sum=
0;
int i;
int cnt=
0;
for(i=
0;i<r;i++)
{
int tx,ty;
tx=findd(edge[i].u);
ty=findd(edge[i].v);
if(tx!=ty)
{
sum+=edge[i].w;
pre[tx]=ty;
cnt++;
}
if(cnt>=n+m-
1)
break;
}
return sum;
}
int main ()
{
int mm;
scanf(
"%d",&mm);
while(mm--)
{
scanf(
"%d%d",&n,&r);
int i;
for(i=
0;i<=n;i++)
pre[i]=i;
for(i=
0;i<r;i++)
{
int u,v,w;
scanf(
"%d%d%d",&u,&v,&w);
edge[i].u=u;
edge[i].v=v;
edge[i].w=w;
}
sort(edge,edge+r,cmp);
int ans;
ans=zyz();
printf(
"%d\n",ans);
}
}