#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define mp				make_pair
#define pb				push_back
#define all(a)			(a).begin(), (a).end()
#define sz(a)			a.size()
#define srt(a)			sort(all(a))
#define mem(a, h)		memset(a, (h), sizeof(a))
#define f 				first
#define s 				second
#define fore(i, b, e)	for(int i = b; i < e; i++)
#define forg(i, b, e, m)	for(int i = b; i < e; i+=m)
//int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;
//using namespace __gnu_pbds;

typedef long long 		ll;
typedef unsigned long long 		ull;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//find_by_order kth largest  order_of_key <
const int tam = 100010;
const int MOD = 1e9+7;
const int MOD1 = 988244535;
const double EPS = 1e-9;
const double PI = acos(-1);
int kp;
vector<int> g[500050];
vector<int> gp[500050];
ll f(int node, int par, int us)
{
	int k = kp - us;
	vector<pair<int, pair<int,int>>> reses;
	ll a, b;
	fore(i, 0, g[node].size())
	{
		if(g[node][i] != par)
		{
			a = f(g[node][i], node, 0);
			b = f(g[node][i], node, 1) + gp[node][i];
			reses.pb(mp(b - a, mp(a, b)));
		}
	}
	if(node == -1)
	{
		cout<<'#'<<k<<'\n';
		fore(i, 0, reses.size())
		cout<<reses[i].f<<' '<<reses[i].s.f<<' '<<reses[i].s.s<<'\n';
	}
	sort(reses.begin(), reses.end());
	reverse(reses.begin(), reses.end());
	int po = 0;
	while(po < reses.size() && reses[po].f > 0 && po < k)
	{
		reses[po].s.f = reses[po].s.s;
		po++;
	}
	if(node == -1)
	{
		fore(i, 0, reses.size())
		cout<<reses[i].f<<' '<<reses[i].s.f<<' '<<reses[i].s.s<<'\n';
	}
	ll res = 0;
	fore(i, 0, reses.size())
		res += reses[i].s.f;
	return res;
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	//freopen("qwe.txt", "r", stdin);
	//freopen("asd.txt", "w", stdout);
	ll res;
	int cas;
	int n;
	cin>>cas;
	int a, b, w;
	while(cas--)
	{
		cin>>n>>kp;
		//cout<<'#'<<kp<<'\n';
		fore(i, 0, n) g[i].clear(), gp[i].clear();
		fore(i, 0, n-1)
		{
			cin>>a>>b>>w;
			a--; b--;
			g[a].pb(b);
			g[b].pb(a);
			gp[a].pb(w);
			gp[b].pb(w);
		}
		cout<<f(0, -1, 0)<<'\n';
	}
	return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define mp				make_pair
#define pb				push_back
#define all(a)			(a).begin(), (a).end()
#define sz(a)			a.size()
#define srt(a)			sort(all(a))
#define mem(a, h)		memset(a, (h), sizeof(a))
#define f 				first
#define s 				second
#define fore(i, b, e)	for(int i = b; i < e; i++)
#define forg(i, b, e, m)	for(int i = b; i < e; i+=m)
//int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;
//using namespace __gnu_pbds;

typedef long long 		ll;
typedef unsigned long long 		ull;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//find_by_order kth largest  order_of_key <
const int tam = 100010;
const int MOD = 1e9+7;
const int MOD1 = 988244535;
const double EPS = 1e-9;
const double PI = acos(-1);
int kp;
vector<int> g[500050];
vector<int> gp[500050];
ll dp[2][500050];
ll f(int node, int par, int us)
{
	if(dp[us][node] != -1) return dp[us][node];
	int k = kp - us;
	vector<pair<int, pair<int,int>>> reses;
	ll a, b;
	fore(i, 0, g[node].size())
	{
		if(g[node][i] != par)
		{
			a = f(g[node][i], node, 0);
			b = f(g[node][i], node, 1) + gp[node][i];
			reses.pb(mp(b - a, mp(a, b)));
		}
	}
	if(node == -1)
	{
		cout<<'#'<<k<<'\n';
		fore(i, 0, reses.size())
		cout<<reses[i].f<<' '<<reses[i].s.f<<' '<<reses[i].s.s<<'\n';
	}
	sort(reses.begin(), reses.end());
	reverse(reses.begin(), reses.end());
	int po = 0;
	while(po < reses.size() && reses[po].f > 0 && po < k)
	{
		reses[po].s.f = reses[po].s.s;
		po++;
	}
	if(node == -1)
	{
		fore(i, 0, reses.size())
		cout<<reses[i].f<<' '<<reses[i].s.f<<' '<<reses[i].s.s<<'\n';
	}
	ll res = 0;
	fore(i, 0, reses.size())
		res += reses[i].s.f;
	return dp[us][node] = res;
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	//freopen("qwe.txt", "r", stdin);
	//freopen("asd.txt", "w", stdout);
	ll res;
	int cas;
	int n;
	cin>>cas;
	int a, b, w;
	while(cas--)
	{
		cin>>n>>kp;
		//cout<<'#'<<kp<<'\n';
		fore(i, 0, n) {g[i].clear(), gp[i].clear();dp[0][i] = dp[1][i] = -1;}
		fore(i, 0, n-1)
		{
			cin>>a>>b>>w;
			a--; b--;
			g[a].pb(b);
			g[b].pb(a);
			gp[a].pb(w);
			gp[b].pb(w);
		}
		cout<<f(0, -1, 0)<<'\n';
	}
	return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
struct edge{
     int to, c;
};

const int MAXN=500007;
vector<edge>g[MAXN];
int mx0[MAXN];
int mx1[MAXN];
int n, k;

void dfs(int r, int p){
     vector<int>temp;
     int ans=0;
     for(int i=0;i<g[r].size();i++){
          int c=g[r][i].to;
          if(c==p)continue;
          dfs(c, r);
          ans+=mx0[c];
          temp.push_back(mx1[c]+g[r][i].c-mx0[c]);
     }
     sort(temp.begin(), temp.end(), greater<long long>());

     for(int i=0;i<min(k-1, (int)temp.size());i++){
          if(temp[i]>0){
               ans=ans+temp[i];
          }

         else break;
     }
     mx1[r]=mx0[r]=ans;

     if((int)temp.size()>=k and temp[k-1]>0) mx0[r]+=temp[k-1];
     //cout<<r<<" "<<mx0[r]<<" "<<mx1[r]<<endl;
     return;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int q;
     cin>>q;
     while(q--){
          for(int i=1;i<=n;i++)g[i].clear();
          cin>>n>>k;
          for(int i=0;i<n-1;i++){
               int a, b, c;
               cin>>a>>b>>c;
               g[a].push_back({b, c});
               g[b].push_back({a, c});
          }
          dfs(1, -1);
          cout<<mx0[1]<<endl;
     }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
struct edge{
     int to, c;
};

const int MAXN=500007;
vector<edge>g[MAXN];
int mx0[MAXN];
int mx1[MAXN];
int n, k;

void dfs(int r, int p){
     vector<int>temp;
     int ans=0;
     for(int i=0;i<g[r].size();i++){
          int c=g[r][i].to;
          if(c==p)continue;
          dfs(c, r);
          ans+=mx0[c];
          temp.push_back(mx1[c]+g[r][i].c-mx0[c]);
     }
     sort(temp.begin(), temp.end(), greater<long long>());

     for(int i=0;i<min(k-1, (int)temp.size());i++){
          if(temp[i]>0){
               ans=ans+temp[i];
          }

         else break;
     }
     mx1[r]=mx0[r]=ans;

     if((int)temp.size()>=k and temp[k-1]>0) mx0[r]+=temp[k-1];
     //cout<<r<<" "<<mx0[r]<<" "<<mx1[r]<<endl;
     return;
}

int32_t main(){
     int q;
     cin>>q;
     while(q--){
          for(int i=1;i<=n;i++)g[i].clear();

          cin>>n>>k;
          for(int i=0;i<n-1;i++){
               int a, b, c;
               cin>>a>>b>>c;
               g[a].push_back({b, c});
               g[b].push_back({a, c});
          }
          dfs(1, -1);
          cout<<mx0[1]<<endl;
     }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
struct edge{
     int to, c;
};

const int MAXN=500007;
vector<edge>g[MAXN];
int mx0[MAXN];
int mx1[MAXN];
int n, k;

bool comp(const pair<int, int>A, const pair<int, int> B){
    if(A.first>B.first) return true;
    return false;
}

void dfs(int r, int p){
     vector<pair<int, int>>temp;
     int ans=0;
     for(int i=0;i<g[r].size();i++){
          int c=g[r][i].to;
          if(c==p)continue;
          dfs(c, r);
          ans+=mx0[c];
          temp.push_back({mx1[c]+g[r][i].c-mx0[c], c});
     }
     sort(temp.begin(), temp.end(), comp);
     for(int i=0;i<min(k-1, (int)temp.size());i++){
          if(temp[i].first>0){
               ans=ans+temp[i].first;
          }
          break;
     }
     mx1[r]=mx0[r]=ans;

     if((int)temp.size()>=k and temp[k-1].first>0) mx0[r]+=temp[k-1].first;
     //cout<<r<<" "<<mx0[r]<<" "<<mx1[r]<<endl;
     return;
}

int32_t main(){
     int q;
     cin>>q;
     while(q--){
          cin>>n>>k;
          for(int i=0;i<n-1;i++){
               int a, b, c;
               cin>>a>>b>>c;
               g[a].push_back({b, c});
               g[b].push_back({a, c});
          }
          memset(mx0, 0, sizeof(int)*(n+10));
          memset(mx1, 0, sizeof(int)*(n+10));
          for(int i=1;i<=n;i++)g[i].clear();
          dfs(1, -1);
          cout<<mx0[1];
     }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
struct edge{
     int to, c;
};

const int MAXN=500007;
vector<edge>g[MAXN];
int mx0[MAXN];
int mx1[MAXN];
int n, k;

bool comp(const pair<int, int>A, const pair<int, int> B){
    if(A.first>B.first) return true;
    return false;
}

void dfs(int r, int p){
     vector<pair<int, int>>temp;
     int ans=0;
     for(int i=0;i<g[r].size();i++){
          int c=g[r][i].to;
          if(c==p)continue;
          dfs(c, r);
          ans+=mx0[c];
          temp.push_back({mx1[c]+g[r][i].c-mx0[c], c});
     }
     sort(temp.begin(), temp.end(), comp);
     for(int i=0;i<min(k-1, (int)temp.size());i++){
          if(temp[i].first>0){
               ans=ans+temp[i].first;
          }
          break;
     }
     mx1[r]=mx0[r]=ans;

     if((int)temp.size()>=k and temp[k-1].first>0) mx0[r]+=temp[k-1].first;
     //cout<<r<<" "<<mx0[r]<<" "<<mx1[r]<<endl;
     return;
}

int32_t main(){
     int q;
     cin>>q;
     while(q--){
          memset(mx0, 0, sizeof(int)*(n+10));
          memset(mx1, 0, sizeof(int)*(n+10));
          for(int i=1;i<=n;i++)g[i].clear();

          cin>>n>>k;
          for(int i=0;i<n-1;i++){
               int a, b, c;
               cin>>a>>b>>c;
               g[a].push_back({b, c});
               g[b].push_back({a, c});
          }
          dfs(1, -1);
          cout<<mx0[1]<<endl;
     }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5+11;
int dp[2][N];
vector<pair<int,int>> graph[N];
int n,k;
void dfs(int u,int par){
    set<pair<int,int>> Set;
    for(auto x:graph[u]){
        int v = x.first;
        int w = x.second;
        if(v!=par){
            dfs(v,u);
            Set.insert({-(w+dp[0][v]-dp[1][v]),v});
        }
    }
    int cnt=1;
    int sum1=0;
    int sum2=0;
    auto it = Set.begin();
    while(it!=Set.end()){
        int diff = it->first;
        int v = it->second;
        if(cnt<k && diff<0){
            diff = abs(diff);
            sum1 = sum1 + dp[1][v] + diff;
            sum2 = sum2 + dp[1][v] + diff;
        }
        else if(cnt==k && diff<0){
            diff = abs(diff);
            sum1 = sum1 + dp[1][v];
            sum2 = sum2 + dp[1][v] + diff;
        }
        else{
            sum1 = sum1 + dp[1][v];
            sum2 = sum2 + dp[1][v];
        }
        cnt++;
        it++;
    }
    //cout<<sum1<<" "<<sum2<<" "<<u<<endl;
    dp[0][u] = sum1;
    dp[1][u] = sum2;
}
int32_t main(){
    ios::sync_with_stdio(false);
    int q;
    cin>>q;
    while(q--){
        cin>>n>>k;
        for(int i=1;i<n;++i){
            int u,v,w;
            cin>>u>>v>>w;
            graph[u].push_back({v,w});
            graph[v].push_back({u,w});
        }
        dfs(1,-1);
        cout<<max(dp[0][1],dp[1][1])<<endl;
        for(int i=1;i<=n;++i){
            dp[0][i] = 0;
            dp[1][i] = 0;
            graph[i].clear();
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5+11;
int dp[2][N];
vector<pair<int,int>> graph[N];
int n,k;
void dfs(int u,int par){
    set<pair<int,int>> Set;
    for(auto x:graph[u]){
        int v = x.first;
        int w = x.second;
        if(v!=par){
            dfs(v,u);
            Set.insert({-(w+dp[0][v]-dp[1][v]),v});
        }
    }
    int cnt=1;
    int sum1=0;
    int sum2=0;
    auto it = Set.begin();
    while(it!=Set.end()){
        int diff = abs(it->first);
        int v = it->second;
        if(cnt<k){
            sum1 = sum1 + dp[1][v] + diff;
            sum2 = sum2 + dp[1][v] + diff;
        }
        else if(cnt==k){
            sum1 = sum1 + dp[1][v];
            sum2 = sum2 + dp[1][v] + diff;
        }
        else{
            sum1 = sum1 + dp[1][v];
            sum2 = sum2 + dp[1][v];
        }
        cnt++;
        it++;
    }
    //cout<<sum1<<" "<<sum2<<" "<<u<<endl;
    dp[0][u] = sum1;
    dp[1][u] = sum2;
}
int32_t main(){
    ios::sync_with_stdio(false);
    int q;
    cin>>q;
    while(q--){
        cin>>n>>k;
        for(int i=1;i<n;++i){
            int u,v,w;
            cin>>u>>v>>w;
            graph[u].push_back({v,w});
            graph[v].push_back({u,w});
        }
        dfs(1,-1);
        cout<<max(dp[0][1],dp[1][1])<<endl;
        for(int i=1;i<=n;++i){
            dp[0][i] = 0;
            dp[1][i] = 0;
            graph[i].clear();
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;

typedef pair<int, int> II;
typedef long long LL;

const int N = 5e5 + 10;

int n, k;
LL f[N][2];
vector<II> adj[N];

void dfs(int u, int par) {
    vector<int> child;
    for (II pack: adj[u])
        if (pack.X != par) {
            dfs(pack.X, u);
            f[pack.X][1] += pack.Y;
            if (f[pack.X][0] >= f[pack.X][1]) {
                f[u][0] += f[pack.X][0];
                f[u][1] += f[pack.X][0];
            } else
                child.push_back(pack.X);
        }
    sort(child.begin(), child.end(), [](const int &it1, const int &it2) {
            return (f[it1][1] - f[it1][0]) > (f[it2][1] - f[it2][0]);
        });
    int cnt = 0;
    for (int v: child) {
        cnt++;
        f[u][1] += f[v][cnt < k];
        f[u][0] += f[v][cnt <= k];
    }
}

void Solve() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
        adj[i].clear();
        f[i][0] = f[i][1] = 0;
    }
    for (int i = 1; i < n; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        adj[x].push_back({y, z});
        adj[y].push_back({x, z});
    }
    dfs(1, 1);
    LL res = 0;
    for (int i = 1; i <= n; i++)
        res = max(res, max(f[i][0], f[i][1]));
    printf("%lld\n", res);
}

int main() {
    //freopen("E.inp", "r", stdin);
    //freopen("E.out", "w", stdout);
    int TC;
    scanf("%d", &TC);
    while (TC--)
        Solve();
}


#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;

typedef pair<int, int> II;
typedef long long LL;

const int N = 5e5 + 10;

int n, k;
LL f[N][2];
vector<II> adj[N];

void dfs(int u, int par) {
    vector<int> child;
    for (II pack: adj[u])
        if (pack.X != par) {
            dfs(pack.X, u);
            f[pack.X][1] += pack.Y;
            child.push_back(pack.X);
        }
    sort(child.begin(), child.end(), [](const int &it1, const int &it2) {
            return (f[it1][1] - f[it1][0]) > (f[it2][1] - f[it2][0]);
        });
    int cnt = 0;
    for (int v: child) {
        cnt++;
        f[u][1] += f[v][cnt < k];
        f[u][0] += f[v][cnt <= k];
    }
}

void Solve() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
        adj[i].clear();
        f[i][0] = f[i][1] = 0;
    }
    for (int i = 1; i < n; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        adj[x].push_back({y, z});
        adj[y].push_back({x, z});
    }
    dfs(1, 1);
    printf("%lld\n", max(f[1][0], f[1][1]));
}

int main() {
    //freopen("E.inp", "r", stdin);
    //freopen("E.out", "w", stdout);
    int TC;
    scanf("%d", &TC);
    while (TC--)
        Solve();
}


#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;

typedef pair<int, int> II;
typedef long long LL;

const int N = 5e5 + 10;

int n, k;
LL f[N][2];
vector<II> adj[N];

void dfs(int u, int par) {
    vector<int> child;
    for (II pack: adj[u])
        if (pack.X != par) {
            dfs(pack.X, u);
            f[pack.X][1] += pack.Y;
            child.push_back(pack.X);
        }
    sort(child.begin(), child.end(), [](const int &it1, const int &it2) {
            return (f[it1][1] - f[it1][0]) > (f[it2][1] - f[it2][0]);
        });
    int cnt = 0;
    for (int v: child) {
        cnt++;
        f[u][1] += f[v][cnt < k];
        f[u][0] += f[v][cnt <= k];
    }
}

void Solve() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
        adj[i].clear();
        f[i][0] = f[i][1] = 0;
    }
    for (int i = 1; i < n; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        adj[x].push_back({y, z});
        adj[y].push_back({x, z});
    }
    dfs(1, 1);
    LL res = 0;
    for (int i = 1; i <= n; i++)
        res = max(res, max(f[i][0], f[i][1]));
    printf("%lld\n", res);
}

int main() {
    //freopen("E.inp", "r", stdin);
    //freopen("E.out", "w", stdout);
    int TC;
    scanf("%d", &TC);
    while (TC--)
        Solve();
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=6e5+5;
typedef long long ll;
const ll inf=1000000000000000;
struct node{
	ll w,h;
	int u;
	bool operator <(const node &rhs)const{
		return rhs.w-rhs.h> w-h;
	}
};
vector<int>g[maxn];
vector<int>w[maxn];
ll d[maxn][2];
int n,k;
void dfs(int u,int fa){
	priority_queue<node> qu;
	int len=g[u].size();
	for(int i=0;i<len;i++){
		int v=g[u][i];
		if(v==fa) continue;
		dfs(v,u);
		qu.push(node{d[v][1]+w[u][i],d[v][0],v});
	}
	int co=0;
	ll res=0;
	node q={inf,0};
	ll mins=inf;
	while(!qu.empty()){
		node p=qu.top();
		qu.pop();
		if(p.w<p.h||co==k) {
			res+=p.h;
		}
		else if(co<k){
			if(p.w<q.w) q=p;
			co++;
			res+=p.w;
		}
	}
	if(co==k){
		d[u][0]=res;
		d[u][1]=res-q.w+q.h;
	}
	else {
		d[u][0]=res;
		d[u][1]=res;
	}
}
int main(){
	int q;
	ios::sync_with_stdio(false);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v,wi;
			cin>>u>>v>>wi;
			g[u].push_back(v);
			g[v].push_back(u);
			w[u].push_back(wi);
			w[v].push_back(wi);
		}
		dfs(1,0);
		cout<<max(d[1][0],d[1][1])<<endl;
		for(int i=0;i<=n;i++) d[i][0]=d[i][1]=0,g[i].clear(),w[i].clear();
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=6e5+5;
typedef long long ll;
const ll inf=1000000000000000;
struct node{
	ll w,h;
	int u;
	bool operator <(const node &rhs)const{
		if(rhs.w-rhs.h==w-h)return rhs.w>w;
		return rhs.w-rhs.h> w-h;
	}
};
vector<int>g[maxn];
vector<int>w[maxn];
ll d[maxn][2];
int n,k;
void dfs(int u,int fa){
	priority_queue<node> qu;
	int len=g[u].size();
	for(int i=0;i<len;i++){
		int v=g[u][i];
		if(v==fa) continue;
		dfs(v,u);
		qu.push(node{d[v][1]+w[u][i],d[v][0]});
	}
	int co=0;
	ll res=0;
	node q={inf,0};
	while(!qu.empty()){
		node p=qu.top();
		qu.pop();
		if(p.w<p.h||co==k) {
			res+=p.h;
		}
		else if(co<k){
			if(p.w<q.w) q=p;
			co++;
			res+=p.w;
		}
	}
	if(co==k){
		d[u][0]=res;
		d[u][1]=res-q.w+q.h;
	}
	else {
		d[u][0]=res;
		d[u][1]=res;
	}
}
int main(){
	int q;
	ios::sync_with_stdio(false);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v,wi;
			cin>>u>>v>>wi;
			g[u].push_back(v);
			g[v].push_back(u);
			w[u].push_back(wi);
			w[v].push_back(wi);
		}
		dfs(1,0);
		cout<<max(d[1][0],d[1][1])<<endl;
		for(int i=0;i<=n;i++) d[i][0]=d[i][1]=0,g[i].clear(),w[i].clear();
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=6e5+5;
typedef long long ll;
const ll inf=1000000000000000;
struct node{
	ll w,h;
	int u;
	bool operator <(const node &rhs)const{
		if(rhs.w-rhs.h==w-h)return rhs.w>w;
		return rhs.w-rhs.h> w-h;
	}
};
vector<int>g[maxn];
vector<int>w[maxn];
ll d[maxn][2];
int n,k;
void dfs(int u,int fa){
	priority_queue<node> qu;
	int len=g[u].size();
	for(int i=0;i<len;i++){
		int v=g[u][i];
		if(v==fa) continue;
		dfs(v,u);
		qu.push(node{d[v][1]+w[u][i],d[v][0]});
	}
	int co=0;
	ll res=0;
	node q={inf,0};
	while(!qu.empty()){
		node p=qu.top();
		qu.pop();
		if(p.w<p.h||co==k) {
			res+=p.h;
		}
		else if(co<k){
			co++;
			if(co==k) q=p;
			res+=p.w;
		}
	}
	if(co==k){
		d[u][0]=res;
		d[u][1]=res-q.w+q.h;
	}
	else {
		d[u][0]=res;
		d[u][1]=res;
	}
}
int main(){
	int q;
	ios::sync_with_stdio(false);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v,wi;
			cin>>u>>v>>wi;
			g[u].push_back(v);
			g[v].push_back(u);
			w[u].push_back(wi);
			w[v].push_back(wi);
		}
		dfs(1,0);
		cout<<max(d[1][0],d[1][1])<<endl;
		for(int i=0;i<=n;i++) d[i][0]=d[i][1]=0,g[i].clear(),w[i].clear();
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=6e5+5;
typedef long long ll;
const ll inf=1000000000000000;
struct node{
	ll w,h;
	int u;
	bool operator <(const node &rhs)const{
		return rhs.w-rhs.h> w-h;
	}
};
vector<int>g[maxn];
vector<int>w[maxn];
ll d[maxn][2];
int n,k;
void dfs(int u,int fa){
	priority_queue<node> qu;
	int len=g[u].size();
	for(int i=0;i<len;i++){
		int v=g[u][i];
		if(v==fa) continue;
		dfs(v,u);
		qu.push(node{d[v][1]+w[u][i],d[v][0],v});
	}
	int co=0;
	ll res=0;
	node q={inf,0};
	ll mins=inf;
	while(!qu.empty()){
		node p=qu.top();
		qu.pop();
		//cout<<p.w<<' '<<p.h<<' '<<p.u<<endl;
		if(p.w<p.h) {
			res+=p.h;
		//	cout<<res<<endl;
		}
		else if(co<k){
			if(p.w<q.w) q=p;
			co++;
			res+=p.w;
		//	cout<<res<<endl;
		}
		else {
			res+=p.h;
		//	cout<<res<<endl;
		}
	}
	if(co==k){
		d[u][0]=res;
		d[u][1]=res-q.w+q.h;
	}
	else {
		d[u][0]=res;
		d[u][1]=res;
	}
}
int main(){
	int q;
	ios::sync_with_stdio(false);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v,wi;
			cin>>u>>v>>wi;
			g[u].push_back(v);
			g[v].push_back(u);
			w[u].push_back(wi);
			w[v].push_back(wi);
		}
		dfs(1,0);
		cout<<max(d[1][0],d[1][1])<<endl;
		for(int i=0;i<=n;i++) d[i][0]=d[i][1]=0,g[i].clear(),w[i].clear();
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=6e5+5;
typedef long long ll;
const ll inf=1000000000000000;
struct node{
	ll w,h;
	int u;
	bool operator <(const node &rhs)const{
		if(rhs.w-rhs.h==w-h)return rhs.w>w;
		return rhs.w-rhs.h> w-h;
	}
};
vector<int>g[maxn];
vector<int>w[maxn];
ll d[maxn][2];
int n,k;
void dfs(int u,int fa){
	priority_queue<node> qu;
	int len=g[u].size();
	for(int i=0;i<len;i++){
		int v=g[u][i];
		if(v==fa) continue;
		dfs(v,u);
		qu.push(node{d[v][1]+w[u][i],d[v][0],v});
	}
	int co=0;
	ll res=0;
	node q={inf,0};
	ll mins=inf;
	while(!qu.empty()){
		node p=qu.top();
		qu.pop();
		if(p.w<p.h||co==k) {
			res+=p.h;
		}
		else if(co<k){
			if(p.w<q.w) q=p;
			co++;
			res+=p.w;
		}
	}
	if(co==k){
		d[u][0]=res;
		d[u][1]=res-q.w+q.h;
	}
	else {
		d[u][0]=res;
		d[u][1]=res;
	}
}
int main(){
	int q;
	ios::sync_with_stdio(false);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i=1;i<n;i++){
			int u,v,wi;
			cin>>u>>v>>wi;
			g[u].push_back(v);
			g[v].push_back(u);
			w[u].push_back(wi);
			w[v].push_back(wi);
		}
		dfs(1,0);
		cout<<max(d[1][0],d[1][1])<<endl;
		for(int i=0;i<=n;i++) d[i][0]=d[i][1]=0,g[i].clear(),w[i].clear();
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=500010;
typedef long long LL;

int n,k;
struct edge{
	int a,b,w;
	bool operator <(const edge &E)const{
		return w>E.w;
	}
}e[N*2];
int in[N];

int main(){
#ifdef WK
	freopen("in.txt","r",stdin);
#endif
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++)in[i]=0;

		for(int i=1;i<n;i++){
			scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].w);
		}

		sort(e+1,e+n);
		LL ans=0;
		for(int i=1;i<n;i++){
			int aa=e[i].a,bb=e[i].b;
			if(in[aa]>=k||in[bb]>=k)continue;
			in[aa]++;in[bb]++;
			ans+=e[i].w;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=500010;
typedef long long LL;

int n,k;
struct edge{
	int a,b,w;
	bool operator <(const edge &E)const{
		return w>E.w;
	}
}e[N*2];
int in[N];

int main(){
#ifdef WK
	freopen("in.txt","r",stdin);
#endif
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++)in[i]=0;

		for(int i=1;i<n;i++){
			scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].w);
		}

		sort(e+1,e+n);
		LL ans=0;
		for(int i=1;i<=n;i++){
			int aa=e[i].a,bb=e[i].b;
			if(in[aa]>=k||in[bb]>=k)continue;
			in[aa]++;in[bb]++;
			ans+=e[i].w;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=500010;
typedef long long LL;
int n,k;
struct edge{
	int b,nt,w;
}e[N*2];
int p[N],nn;
void anode(int x,int y,int z){
	nn++;e[nn].b=y;e[nn].nt=p[x];p[x]=nn;e[nn].w=z;
	swap(x,y);
	nn++;e[nn].b=y;e[nn].nt=p[x];p[x]=nn;e[nn].w=z;
}

LL f[N][2];
int w[N];
int cmp(int x,int y){
	return f[x][1]+w[x]-f[x][0]>f[y][1]+w[y]-f[y][0];
}

void dfs(int x,int fa=0){
	vector<int>S;
	for(int i=p[x];i;i=e[i].nt){
		int t=e[i].b;
		if(t==fa)continue;
		S.push_back(t);
		w[t]=e[i].w;
		dfs(t,x);
	}

	sort(S.begin(),S.end(),cmp);

	for(int i=0,sz=S.size();i<sz;i++){
		int t=S[i];
		if(f[t][1]+w[t]<=f[t][0]){
			f[x][0]+=f[t][0];
			f[x][1]+=f[t][0];
			continue;
		}
		if(i<k-1){
			f[x][0]+=f[t][1]+w[t];
			f[x][1]+=f[t][1]+w[t];
		}else if(i==k-1){
			f[x][0]+=f[t][1]+w[t];
			f[x][1]+=f[t][0];
		}else{
			f[x][0]+=f[t][0];
			f[x][1]+=f[t][0];
		}
	}
}

int main(){
#ifdef WK
	freopen("in.txt","r",stdin);
#endif
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++)p[i]=0,f[i][0]=f[i][1]=0;
		nn=0;
		for(int i=1;i<n;i++){
			int aa,bb,cc;
			scanf("%d%d%d",&aa,&bb,&cc);
			anode(aa,bb,cc);
		}
		dfs(1);
		printf("%lld\n",f[1][0]);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
#define pi pair<int,int>
#define x first
#define y second

using namespace std;

const int nmax = 5e5 + 5;

vector<pi>l[nmax];
ll dp[nmax][2];
int aux[nmax];

bool mycmp(ll a, ll b) {
	return a > b;
}

void solve(int nod, int tata,int ke) {
	ll ans = 0;
	vector<ll>aux;
	for (auto z : l[nod]) {
		if (tata == z.x)
			continue;
		solve(z.x, nod, ke);
		ans += dp[z.x][0];
		aux.push_back(dp[z.x][1] - dp[z.x][0] + z.y);
	}
	sort(aux.begin(), aux.end(), mycmp);
	for (int i = 0; i < ke and i <(int)aux.size(); i++)
		if (aux[i] > 0)
			ans += aux[i];
	dp[nod][0] = ans;
	if(ke <= (int)aux.size() and aux[ke-1] > 0)
		dp[nod][1] = ans - aux[ke-1];
	else
		dp[nod][1]=ans;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		for (int i = 1; i < n; i++) {
			int a, b, w;
			cin >> a >> b >> w;
			l[a].push_back({ b,w });
			l[b].push_back({ a,w });
		}
		solve(1, 0, k);
		cout << dp[1][0] << "\n";
		for (int i = 1; i <= n; i++)
			l[i].clear();
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
#define pi pair<int,int>
#define x first
#define y second

using namespace std;

const int nmax = 5e5 + 5;

vector<pi>l[nmax];
ll dp[nmax][2];
int aux[nmax];

bool mycmp(ll a, ll b) {
	return a > b;
}

void solve(int nod, int tata,int ke) {
	ll ans = 0;
	vector<ll>aux;
	for (auto z : l[nod]) {
		if (tata == z.x)
			continue;
		solve(z.x, nod, ke);
		ans += dp[z.x][0];
		aux.push_back(dp[z.x][1] - dp[z.x][0] + z.y);
	}
	sort(aux.begin(), aux.end(), mycmp);
	for (int i = 0; i < ke and i <(int)aux.size(); i++)
		if (aux[i] > 0)
			ans += aux[i];
	dp[nod][0] = ans;
	if(ke <= (int)aux.size() and aux[ke-1] > 0)
		dp[nod][1] = ans - aux[ke-1];
	else
		dp[nod][1]=ans;
}

int main()
{
	int t;
	cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		for (int i = 1; i < n; i++) {
			int a, b, w;
			cin >> a >> b >> w;
			l[a].push_back({ b,w });
			l[b].push_back({ a,w });
		}
		solve(1, 0, k);
		cout << dp[1][0] << "\n";
		for (int i = 1; i <= n; i++)
			l[i].clear();
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge
{
	int v,w,next;
};
Edge e[1000010];
ll head[500010],cnt;
ll dp[500010][2];
ll n,k;
void addedge(ll u,ll v,ll w)
{
	cnt++;
	e[cnt].v=v;
	e[cnt].w=w;
	e[cnt].next=head[u];
	head[u]=cnt;
}
void dfs(ll u,ll fa)
{
	vector<ll> tmp;
	ll sum=0;
	for(ll i=head[u];i;i=e[i].next)
	{
		ll v=e[i].v,w=e[i].w;
		if(v==fa)continue;
		dfs(v,u);
		sum+=dp[v][0];
		tmp.push_back(dp[v][1]+w-dp[v][0]);
	}
	dp[u][0]=dp[u][1]=sum;
	sort(tmp.begin(),tmp.end());
	ll size=tmp.size();
	ll ito=max(0ll,size-k);
	for(int i=size-1;i>=ito;i--)
	{
		if(tmp[i]<=0)break;
		if(size-i<=k)dp[u][0]+=tmp[i];
		if(size-i<=k-1)dp[u][1]+=tmp[i];
	}
}
int main()
{
	ll q;
	scanf("%I64d",&q);
	while(q--)
	{
		scanf("%I64d%I64d",&n,&k);
		for(ll i=1;i<=n;i++)head[i]=0;
		cnt=0;
		for(ll i=1;i<n;i++)
		{
			ll u,v,w;
			scanf("%I64d%I64d%I64d",&u,&v,&w);
			addedge(u,v,w);
			addedge(v,u,w);
		}
		dfs(1,0);
		printf("%I64d\n",dp[1][0]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge
{
	int v,w,next;
};
Edge e[1000010];
int head[500010],cnt;
int dp[500010][2];
int n,k;
void addedge(int u,int v,int w)
{
	cnt++;
	e[cnt].v=v;
	e[cnt].w=w;
	e[cnt].next=head[u];
	head[u]=cnt;
}
void dfs(int u,int fa)
{
	vector<int> tmp;
	int sum=0;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].v,w=e[i].w;
		if(v==fa)continue;
		dfs(v,u);
		sum+=dp[v][0];
		tmp.push_back(dp[v][1]+w-dp[v][0]);
	}
	dp[u][0]=dp[u][1]=sum;
	sort(tmp.begin(),tmp.end());
	int size=tmp.size();
	int ito=max(0,size-k);
	for(int i=size-1;i>=ito;i--)
	{
		if(tmp[i]<=0)break;
		if(size-i<=k)dp[u][0]+=tmp[i];
		if(size-i<=k-1)dp[u][1]+=tmp[i];
	}
}
int main()
{
	int q;
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++)head[i]=0;
		cnt=0;
		for(int i=1;i<n;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			addedge(u,v,w);
			addedge(v,u,w);
		}
		dfs(1,0);
		printf("%d\n",dp[1][0]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge
{
	int v,w,next;
};
Edge e[1000010];
int head[500010],cnt;
int dp[500010][2];
int n,k;
void addedge(int u,int v,int w)
{
	cnt++;
	e[cnt].v=v;
	e[cnt].w=w;
	e[cnt].next=head[u];
	head[u]=cnt;
}
void dfs(int u,int fa)
{
	vector<int> tmp;
	int sum=0;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].v,w=e[i].w;
		if(v==fa)continue;
		dfs(v,u);
		sum+=dp[v][0];
		tmp.push_back(dp[v][1]+w-dp[v][0]);
	}
	dp[u][0]=dp[u][1]=sum;
	sort(tmp.begin(),tmp.end());
	int size=tmp.size();
	for(int i=size-1;i>=0;i--)
	{
		if(tmp[i]<=0)break;
		if(size-i<=k)dp[u][0]+=tmp[i];
		if(size-i<=k-1)dp[u][1]+=tmp[i];
	}
}
int main()
{
	int q;
	cin>>q;
	while(q--)
	{
		cin>>n>>k;
		for(int i=1;i<=n;i++)head[i]=0;
		cnt=0;
		for(int i=1;i<n;i++)
		{
			int u,v,w;
			cin>>u>>v>>w;
			addedge(u,v,w);
			addedge(v,u,w);
		}
		dfs(1,0);
		cout<<dp[1][0]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define ms(a,b)  memset(a,b,sizeof a)
#define sz(x) (int)x.size()
using ll = long long;
const int inf = 0x3f3f3f3f;
const int maxn = 5e5+5;
const int mod = 1e9+7;
struct  Edge{
    int v,w,nxt;
}edge[2*maxn];
int head[maxn],tot;
void init(int n)
{
    fill(head,head+n+1,-1);
    tot = 0;
}
void addedge(int u,int v,int w)
{
    edge[tot] = {v,w,head[u]};
    head[u] = tot++;
}
int n,k;
ll f[maxn]; // k
ll g[maxn]; // <k
using pll = pair<ll,ll>;
bool cmp(pll &x,pll &y) {
    return x.second - x.first > y.second - y.first;
}
void dfs(int u,int pre) {
    vector<pll> ve;
    for(int i=head[u];~i;i=edge[i].nxt) {
        int v=edge[i].v;
        if(v==pre) continue;
        dfs(v,u);
        ve.push_back({f[v],g[v]+edge[i].w});
    }
    sort(ve.begin(),ve.end(),cmp);
    f[u] = g[u] = 0;
    int cnt = 0;
    for(auto &p:ve) {
        if((++cnt) <= k) f[u] += max(p.first,p.second); else f[u] += p.first;
        if(cnt < k) g[u] += max(p.first,p.second); else g[u] += p.first;
    }
}
int main()
{
    io_speed_up;
    int T;
    cin>>T;
    while(T--) {
        cin>>n>>k;
        init(n);
        rep(i,1,n-1) {
            int u,v,w;
            cin>>u>>v>>w;
            addedge(u,v,w);
            addedge(v,u,w);
        }
        dfs(1,0);
        cout<<f[1]<<"\n";
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <bitset>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=5e5+10;
typedef long long ll;
ll dp[maxn][2];
int n,k,head[maxn],cnt;
struct node{
    int v,w,nxt;
    node(int v=0,int w=0,int nxt=0):v(v),w(w),nxt(nxt){}
}ex[maxn*2];

void add(int u,int v,int w){
    ex[cnt]=node(v,w,head[u]);
    head[u]=cnt++;
    ex[cnt]=node(u,w,head[v]);
    head[v]=cnt++;
}
bool cmp(int a,int b){
    return a>b;
}

void dfs(int u,int pre){
    dp[u][0]=dp[u][1]=0;
    for(int i=head[u];i!=-1;i=ex[i].nxt){
        int v=ex[i].v;
        if(v==pre) continue;
        dfs(v,u);
        dp[u][0]+=dp[v][1];
        dp[u][1]+=dp[v][1];
    }
    vector<int>val;val.clear();
    for(int i=head[u];i!=-1;i=ex[i].nxt){
        int v=ex[i].v;
        if(v==pre) continue;
        val.push_back(dp[v][0]+ex[i].w-dp[v][1]);
    }
    sort(val.begin(),val.end(),cmp);
    int len=val.size();
    len=min(k,len);
    for(int i=0;i<len;i++){
        if(val[i]<0) break;
        if(i<k-1) dp[u][0]+=val[i];
        dp[u][1]+=val[i];
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        cnt=0;
        scanf("%d%d",&n,&k);
        for(int i=0;i<=n;i++) head[i]=-1;
        for(int i=1;i<n;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        dfs(1,-1);
        printf("%lld\n",max(dp[1][0],dp[1][1]));
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 1000000000
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define ri register int
#define rep(i,a,b) for(ri i=(a);i<=(b);++i)
#define rep2(i,a,b,c) for(ri i=(a);i<=(b);i+=(c))
#define REP(i,a,b) for(ri i=(a);i>=(b);--i)
#define REP2(i,a,b,c) for(ri i=(a);i>=(b);i-=(c))
using namespace std;

inline ll read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^'0'),c=getchar();
	return x*f;
}
inline void write(ll x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}

const int N=5e5+5;
vector<pii> gv[N];
int n,k;
ll f[N][2];

inline void dfs(ri u,ri fa){
	rep(i,0,(int)gv[u].size()-1){
		ri v=gv[u][i].fi;if(v==fa) continue;
		dfs(v,u);
	}
	ll s=0;vector<ll>ve;
	rep(i,0,(int)gv[u].size()-1){
		ri v=gv[u][i].fi;if(v==fa) continue;
		s+=f[v][0];
		ve.pb(f[v][1]+gv[u][i].se-f[v][0]);
	}
	sort(ve.begin(),ve.end());reverse(ve.begin(),ve.end());
	f[u][0]=f[u][1]=s;
	for(ri i=0;i<k&&i<(int)ve.size();++i){if(ve[i]<1)break;f[u][0]+=ve[i];}
	for(ri i=0;i<k-1&&i<(int)ve.size();++i){if(ve[i]<1)break;f[u][1]+=ve[i];}
}

int main()
{
	ri q=read();
	while(q--){
		n=read();k=read();
		rep(i,1,n) gv[i].clear();
		rep(i,1,n-1){
			ri u=read(),v=read(),w=read();
			gv[u].pb(mp(v,w));
			gv[v].pb(mp(u,w));
		}
		dfs(1,0);write(f[1][0]);puts("");
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 1000000000
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define ri register int
#define rep(i,a,b) for(ri i=(a);i<=(b);++i)
#define rep2(i,a,b,c) for(ri i=(a);i<=(b);i+=(c))
#define REP(i,a,b) for(ri i=(a);i>=(b);--i)
#define REP2(i,a,b,c) for(ri i=(a);i>=(b);i-=(c))
using namespace std;

inline ll read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^'0'),c=getchar();
	return x*f;
}
inline void write(ll x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}

const int N=3e5+5;
vector<pii> gv[N];
int n,k;
ll f[N][2];

inline void dfs(ri u,ri fa){
	rep(i,0,(int)gv[u].size()-1){
		ri v=gv[u][i].fi;if(v==fa) continue;
		dfs(v,u);
	}
	ll s=0;vector<ll>ve;
	rep(i,0,(int)gv[u].size()-1){
		ri v=gv[u][i].fi;if(v==fa) continue;
		s+=f[v][0];
		ve.pb(f[v][1]+gv[u][i].se-f[v][0]);
	}
	sort(ve.begin(),ve.end());reverse(ve.begin(),ve.end());
	f[u][0]=f[u][1]=s;
	for(ri i=0;i<k&&i<(int)ve.size();++i){if(ve[i]<1)break;f[u][0]+=ve[i];}
	for(ri i=0;i<k-1&&i<(int)ve.size();++i){if(ve[i]<1)break;f[u][1]+=ve[i];}
}

int main()
{
	ri q=read();
	while(q--){
		n=read();k=read();
		rep(i,1,n) gv[i].clear();
		rep(i,1,n-1){
			ri u=read(),v=read(),w=read();
			gv[u].pb(mp(v,w));
			gv[v].pb(mp(u,w));
		}
		dfs(1,0);write(f[1][0]);puts("");
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=5e5+50;
int t,n,k,ver[N*2],edge[N*2],nxt[N*2],head[N],tot,tp;LL f[N][2];
struct node{
	LL f0,f1,d;
	bool friend operator <(node a,node b){return a.d>b.d;}
}st[N];
void add(int x,int y,int z){ver[++tot]=y;edge[tot]=z;nxt[tot]=head[x];head[x]=tot;}
void dfs(int x,int ff){
	int tt=tp;f[x][0]=f[x][1]=0;
	for(int i=head[x],y;i;i=nxt[i]){
		if((y=ver[i])==ff)continue;
		dfs(y,x);LL f0=max(f[y][1],f[y][0]),f1=f[y][0]+edge[i];
		st[++tp]=(node){f0,f1,f1-f0};
	}
	sort(st+tt+1,st+tp+1);
	int i=tt+1;
	for(;i<=min(tp,tt+k-1);i++){
		if(st[i].d<=0)break;
		f[x][0]+=st[i].f1,f[x][1]+=st[i].f1;
	}
	if(i<=tp){
		f[x][0]+=st[i].f0,f[x][1]+=st[i].f1;i++;
		for(;i<=tp;i++)f[x][0]+=st[i].f0,f[x][1]+=st[i].f0;
	}
	tp=tt;
}
void solve(){
	scanf("%d%d",&n,&k);
	for(int i=1,x,y,z;i<n;i++){
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);add(y,x,z);
	}
	dfs(1,0);cout<<max(f[1][0],f[1][1]);
	for(int i=1;i<=n;i++)head[i]=0;tot=0;
}
int main(){
	scanf("%d",&t);
	while(t--)solve();
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=5e5+50;
int t,n,k,ver[N*2],edge[N*2],nxt[N*2],head[N],tot,tp;LL f[N][2];
struct node{
	LL f0,f1,d;
	bool friend operator <(node a,node b){return a.d>b.d;}
}st[N];
void add(int x,int y,int z){ver[++tot]=y;edge[tot]=z;nxt[tot]=head[x];head[x]=tot;}
void dfs(int x,int ff){
	int tt=tp;f[x][0]=f[x][1]=0;
	for(int i=head[x],y;i;i=nxt[i]){
		if((y=ver[i])==ff)continue;
		dfs(y,x);LL f0=max(f[y][1],f[y][0]),f1=f[y][0]+edge[i];
		st[++tp]=(node){f0,f1,f1-f0};
	}
	sort(st+tt+1,st+tp+1);
	int i=tt+1;
	for(;i<=min(tp,tt+k-1);i++){
		if(st[i].d<=0)break;
		f[x][0]+=st[i].f1,f[x][1]+=st[i].f1;
	}
	if(i<=tp){
		f[x][0]+=st[i].f0,f[x][1]+=st[i].f1;i++;
		for(;i<=tp;i++)f[x][0]+=st[i].f0,f[x][1]+=st[i].f0;
	}
	tp=tt;
}
void solve(){
	scanf("%d%d",&n,&k);
	for(int i=1,x,y,z;i<n;i++){
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);add(y,x,z);
	}
	dfs(1,0);cout<<max(f[1][0],f[1][1])<<endl;
	for(int i=1;i<=n;i++)head[i]=0;tot=0;
}
int main(){
	scanf("%d",&t);
	while(t--)solve();
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int Max = 5e5 + 5, Mod = 1e9 + 7;

int n, k;
typedef pair<ll, ll> p2;
p2 dp[Max];
vector<p2> G[Max];
const ll INF = 1e15;

bool cmp(p2& a, p2& b)
{
	return a.first - a.second > b.first - b.second;
}

void Try(int u, int p)
{
	dp[u] = { 0,0 };
	vector<p2> Res;
	ll Sum = 0;
	for (auto v : G[u])
		if (v.first != p)
		{
			Try(v.first, u);
			p2 New = dp[v.first];
			New.first += v.second;
			Sum += New.second;
			Res.push_back(New);
		}
	sort(Res.begin(), Res.end(), cmp);
	dp[u] = { Sum,Sum };
	for (int i = 0; i < Res.size() && i < k; i++)
	{
		Sum += Res[i].first - Res[i].second;
		dp[u].second = max(dp[u].second, Sum);
		if (i + 1 < k)
			dp[u].first = max(dp[u].first, Sum);
	}
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		cin >> n >> k;
		for (int i = 1; i <= n; i++)
			G[i].clear();
		for (int i = 0; i < n - 1; i++)
		{
			int u, v, w;
			//cin >> u >> v >> w;
			scanf("%d%d%d", &u, &v, &w);
			G[u].push_back({ v,w });
			G[v].push_back({ u,w });
		}
		Try(1, 1);
		printf("%I64d\n", dp[1].second);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

long long dpf (int u, int p, bool is_decreased) {
	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			child_answers.push_back({dpf(v, u, 0), dpf(v, u, 1) + w});
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	long long ans = 0;

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// dp

	return ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%I64d\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

map<int, long> long dp[N + 7][2];

long long dpf (int u, int p, bool is_decreased) {
	long long &d = dp[u][is_decreased][p];

	if (d != -1) {
		return d;
	}

	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	long long ans = 0;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			long long child_answer_1 = dpf(v, u, 0);
			long long child_answer_2 = dpf(v, u, 1) + w;

			if (child_answer_2 > child_answer_1) {
				child_answers.push_back({child_answer_1, child_answer_2});
			}
			else {
				ans += child_answer_1;
			}
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// cerr << u << " " << p << " " << is_decreased << " " << ans << "\n";

	return d = ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		memset(dp, -1, sizeof dp);

		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%I64d\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();
		}
	}

	return 0;
}

/*
6 2
3 2 7
1 2 2
2 4 10
2 5 2
4 6 8
*/


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

map<int, long long> dp[N + 7][2];

long long dpf (int u, int p, bool is_decreased) {
	if (dp[u][is_decreased].find(p) != dp[u][is_decreased].end()) {
		return dp[u][is_decreased][p];
	}

	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	long long ans = 0;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			long long child_answer_1 = dpf(v, u, 0);
			long long child_answer_2 = dpf(v, u, 1) + w;

			if (child_answer_2 > child_answer_1) {
				child_answers.push_back({child_answer_1, child_answer_2});
			}
			else {
				ans += child_answer_1;
			}
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// cerr << u << " " << p << " " << is_decreased << " " << ans << "\n";

	return dp[u][is_decreased][p] = ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%I64d\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();

			dp[u][0] = map<int, long long>();
			dp[u][1] = map<int, long long>();
		}
	}

	return 0;
}

/*
6 2
3 2 7
1 2 2
2 4 10
2 5 2
4 6 8
*/


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

long long dpf (int u, int p, bool is_decreased) {
	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			child_answers.push_back({dpf(v, u, 0), dpf(v, u, 1) + w});
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	long long ans = 0;

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// dp

	return ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%lld\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

long long dp[N + 7][2];

long long dpf (int u, int p, bool is_decreased) {
	long long &d = dp[u][is_decreased];

	if (d != -1) {
		return d;
	}

	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	long long ans = 0;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			long long child_answer_1 = dpf(v, u, 0);
			long long child_answer_2 = dpf(v, u, 1) + w;

			if (child_answer_2 > child_answer_1) {
				child_answers.push_back({child_answer_1, child_answer_2});
			}
			else {
				ans += child_answer_1;
			}
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// cerr << u << " " << p << " " << is_decreased << " " << ans << "\n";

	return d = ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		memset(dp, -1, sizeof dp);

		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%I64d\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();
		}
	}

	return 0;
}

/*
6 2
3 2 7
1 2 2
2 4 10
2 5 2
4 6 8
*/


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

long long dpf (int u, int p, bool is_decreased) {
	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	long long ans = 0;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			long long child_answer_1 = dpf(v, u, 0);
			long long child_answer_2 = dpf(v, u, 1) + w;

			if (child_answer_2 > child_answer_1) {
				child_answers.push_back({child_answer_1, child_answer_2});
			}
			else {
				ans += child_answer_1;
			}
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// cerr << u << " " << p << " " << is_decreased << " " << ans << "\n";

	return ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%I64d\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();
		}
	}

	return 0;
}

/*
6 2
3 2 7
1 2 2
2 4 10
2 5 2
4 6 8
*/


#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;

vector<pair<int, int> > edge[N + 7];

int global_k;

map<int, long long> dp[N + 7][2];

long long dpf (int u, int p, bool is_decreased) {
	long long &d = dp[u][is_decreased][p];

	if (d != -1) {
		return d;
	}

	int currk = global_k - is_decreased;

	vector<pair<long long, long long> > child_answers;

	long long ans = 0;

	for (int i = 0; i < edge[u].size(); i++) {
		int v = edge[u][i].first;
		int w = edge[u][i].second;

		if (v == p) {
			continue;
		}
		else {
			long long child_answer_1 = dpf(v, u, 0);
			long long child_answer_2 = dpf(v, u, 1) + w;

			if (child_answer_2 > child_answer_1) {
				child_answers.push_back({child_answer_1, child_answer_2});
			}
			else {
				ans += child_answer_1;
			}
		}
	}

	sort(child_answers.begin(), child_answers.end(), [](pair<long long, long long> p1, pair<long long, long long> p2) {
		if (p1.second == p2.second) {
			return p1.first > p2.first;
		}
		else {
			return p1.second > p2.second;
		}
	});

	multiset<long long> diffs;

	int lasti = -1;

	for (int i = 0; i < min(currk, (int) child_answers.size()); i++) {
		ans += child_answers[i].second;

		diffs.insert(child_answers[i].second - child_answers[i].first);

		lasti = i;
	}

	for (int i = lasti + 1; i < child_answers.size(); i++) {
		if (diffs.size() == 0) {
			ans += child_answers[i].first;
		}
		else if (child_answers[i].second - child_answers[i].first > *diffs.begin()) {
			ans += child_answers[i].second;
			ans -= *diffs.begin();
			diffs.erase(diffs.begin());
			diffs.insert(child_answers[i].second - child_answers[i].first);
		}
		else {
			ans += child_answers[i].first;
		}
	}

	// cerr << u << " " << p << " " << is_decreased << " " << ans << "\n";

	return d = ans;
}

int main () {
	int q;

	cin >> q;

	while (q--) {
		memset(dp, -1, sizeof dp);

		int n, k;

		scanf("%d %d", &n, &k);

		global_k = k;

		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			edge[u].push_back({v, w});
			edge[v].push_back({u, w});
		}

		printf("%I64d\n", dpf(1, 0, 0));

		for (int u = 0; u <= n; u++) {
			edge[u] = vector<pair<int, int> >();
		}
	}

	return 0;
}

/*
6 2
3 2 7
1 2 2
2 4 10
2 5 2
4 6 8
*/


#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
#define N 500011

using namespace std;

typedef pair<int,int> pi;

int n, k;
vector<pi> adj[N];

/* ----- DP ----- */
int opt[2][N];

int f(int v, int p, int typ, int pcost) {
	if(opt[typ][v] != -1) return opt[typ][v];
	int res = pcost;
	int deg = 0;

	vector<int> tmp;
	for(auto x : adj[v]) {
		int u = x.fi;
		int w = x.se;
		if(u == p) continue;
		deg++;
		res += f(u, v, 0, 0);
		tmp.push_back(f(u, v, 1, w) - f(u, v, 0, 0));
	}
	sort(tmp.begin(), tmp.end());
	reverse(tmp.begin(), tmp.end());

	for(int i=0; i<k-typ and i<deg; i++) {
		res += max(0LL, tmp[i]);
	}
	return opt[typ][v] = res;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	cin >> t;
	while(t--) {
		cin >> n >> k;

		// Reset Global Variables
		for(int i=0; i<n; i++) {
			vector<pi>().swap(adj[i]);
			opt[0][i] = opt[1][i] = -1;
		}

		for(int i=1; i<n; i++) {
			int u, v, w;
			cin >> u >> v >> w;
			u--; v--;
			adj[u].emplace_back(v, w);
			adj[v].emplace_back(u, w);
		}
		cout << f(0,0,0,0) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
#define ll long long

using namespace std;

int q, n, k;
ll dp[500010][2];
vector<pii> G[500010];

bool cmp(pii a, pii b)
{
	ll t1 = dp[a.second][1] + a.first - dp[a.second][0];
	ll t2 = dp[b.second][1] + b.first - dp[b.second][0];
	return t1 > t2;
}

void dfs(int p, int fa)
{
	vector<pii> sons;
	for (auto to: G[p])
	{
		if (to.second == fa) continue;
		sons.push_back(to);
		dfs(to.second, p);
	}
	sort(sons.begin(), sons.end(), cmp);
	int cur = n + 1;
	for (int i = 0; i < sons.size(); i++)
	{
		if (i >= k) break;
		int to = sons[i].second;
		ll val = dp[to][1] + sons[i].first;
		if (val - dp[to][0] <= 0)
		{
			cur = i;
			break;
		}
		if (i < k - 1) dp[p][1] += val;
		dp[p][0] += val;
	}
	if (sons.size() < min(cur, k)) return;
	if (cur > n) dp[p][1] += dp[sons[k - 1].second][0];
	for (int i = min(cur, k); i < sons.size(); i++)
	{
		int to = sons[i].second;
		if (to == fa) break;
		dp[p][0] += dp[to][0];
		dp[p][1] += dp[to][0];
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> q;
	while (q--)
	{
		cin >> n >> k;
		for (int i = 1; i < n; i++)
		{
			int a, b, w; cin >> a >> b >> w;
			G[a].push_back(mp(w, b));
			G[b].push_back(mp(w, a));
		}
		dfs(1, 0);
		cout << dp[1][0] << endl;

		for (int i = 1; i <= n; i++)
		{
			dp[i][0] = dp[i][1] = 0;
			G[i].clear();
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
#define ll long long

using namespace std;

int q, n, k;
ll dp[500010][2];
vector<pii> G[500010];

bool cmp(pii a, pii b)
{
	ll t1 = dp[a.second][1] + a.first - dp[a.second][0];
	ll t2 = dp[b.second][1] + b.first - dp[b.second][0];
	return t1 > t2;
}

void dfs(int p, int fa)
{
	vector<pii> sons;
	for (auto to: G[p])
	{
		if (to.second == fa) continue;
		sons.push_back(to);
		dfs(to.second, p);
	}
	sort(sons.begin(), sons.end(), cmp);
	int cur = n + 1;
	for (int i = 0; i < sons.size(); i++)
	{
		if (i >= k) break;
		int to = sons[i].second;
		ll val = dp[to][1] + sons[i].first;
		if (val - dp[to][0] <= 0)
		{
			cur = i;
			break;
		}
		if (i < k - 1) dp[p][1] += val;
		dp[p][0] += val;
	}
	if (sons.size() < k) return;
	if (cur > n) dp[p][1] += dp[sons[k - 1].second][0];
	for (int i = min(cur, k); i < sons.size(); i++)
	{
		int to = sons[i].second;
		if (to == fa) break;
		dp[p][0] += dp[to][0];
		dp[p][1] += dp[to][0];
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> q;
	while (q--)
	{
		cin >> n >> k;
		for (int i = 1; i < n; i++)
		{
			int a, b, w; cin >> a >> b >> w;
			G[a].push_back(mp(w, b));
			G[b].push_back(mp(w, a));
		}
		dfs(1, 0);
		cout << dp[1][0] << endl;

		for (int i = 1; i <= n; i++)
		{
			dp[i][0] = dp[i][1] = 0;
			G[i].clear();
		}
	}
	return 0;
}


#include <cstdio>

#include <cstring>

#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
//#include <queue>
//#include <stack>
#include <cassert>

#include <numeric>

using namespace std;

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<llong> VL;
typedef vector<VL> VVL;

struct Edge {
   int to, w;
};

int N, K;
vector< vector<Edge> > adj;

const llong INF = 1e17;

VL DP_sat;
VL DP_notsat;
void dfs(int u, int p = 0) {
   int nchildren = 0;
   for (Edge e : adj[u]) {
      int v = e.to;
      if (v == p) continue;
      nchildren++;
      dfs(v, u);
   }
   if (nchildren == 0) {
      return;
   }

   if (nchildren < K) {
      // cannot saturate node u
      DP_sat[u] = 0;  // -INF
      DP_notsat[u] = 0;
      for (Edge e : adj[u]) {
         int v = e.to;
         if (v == p) continue;
         DP_notsat[u] += max( DP_sat[v], DP_notsat[v] + e.w );
      }
   }
   else {
      llong base = 0;
      VL gains;
      for (Edge e : adj[u]) {
         int v = e.to;
         if (v == p) continue;
         llong t = max( DP_sat[v], DP_notsat[v] );
         base += t;
         gains.push_back( e.w - t + DP_notsat[v] );
      }
      sort(gains.begin(), gains.end(), greater<llong>());
      assert(SZ(gains) >= K);
      DP_sat[u] = base + accumulate(gains.begin(), gains.begin()+K, 0LL);
      if (DP_sat[u] < 0)
         DP_sat[u] = 0;
/*
      DP_notsat[u] = max(base,
                         base + accumulate(gains.begin(),
                                           gains.begin()+K-1, 0LL));
*/
      DP_notsat[u] = base;
      REP(k, K-1) {
         if (gains[k] <= 0) break;
         DP_notsat[u] += gains[k];
      }
   }
}

int main(int argc, char* argv[]) {
   int TC;
   scanf("%d", &TC);
   FOR(tc, 1, TC) {
      scanf("%d %d", &N, &K);
      adj = vector< vector<Edge> >(N+1);
      REP(j, N-1) {
         int u, v, w;
         scanf("%d %d %d", &u, &v, &w);
         adj[u].push_back({v, w});
         adj[v].push_back({u, w});
      }

      DP_sat = VL(N+1);
      DP_notsat = VL(N+1);
      dfs(1);

      //
      //FOR(u, 1, N)
      // fprintf(stderr, "%d: %lld %lld\n", u, DP_notsat[u], DP_sat[u]);
      //

      llong res = max(DP_sat[1], DP_notsat[1]);
      printf("%lld\n", res);
   }

   return 0;
}


#include <cstdio>

#include <cstring>

#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
//#include <queue>
//#include <stack>
#include <cassert>

#include <numeric>

using namespace std;

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<llong> VL;
typedef vector<VL> VVL;

struct Edge {
   int to, w;
};

int N, K;
vector< vector<Edge> > adj;

const llong INF = 1e17;

VL DP_sat;
VL DP_notsat;
void dfs(int u, int p = 0) {
   int nchildren = 0;
   for (Edge e : adj[u]) {
      int v = e.to;
      if (v == p) continue;
      nchildren++;
      dfs(v, u);
   }
   if (nchildren == 0) {
      return;
   }

   if (nchildren < K) {
      // cannot saturate node u
      DP_sat[u] = 0;  // -INF
      DP_notsat[u] = 0;
      for (Edge e : adj[u]) {
         int v = e.to;
         if (v == p) continue;
         DP_notsat[u] += max( DP_sat[v], DP_notsat[v] + e.w );
      }
   }
   else {
      llong base = 0;
      VL gains;
      for (Edge e : adj[u]) {
         int v = e.to;
         if (v == p) continue;
         llong t = max( DP_sat[v], DP_notsat[v] );
         base += t;
         gains.push_back( e.w - t + DP_notsat[v] );
      }
      sort(gains.begin(), gains.end(), greater<llong>());
      assert(SZ(gains) >= K);
      DP_sat[u] = base + accumulate(gains.begin(), gains.begin()+K, 0LL);
/*
      DP_notsat[u] = max(base,
                         base + accumulate(gains.begin(),
                                           gains.begin()+K-1, 0LL));
*/
      DP_notsat[u] = base;
      REP(k, K-1) {
         if (gains[k] <= 0) break;
         DP_notsat[u] += gains[k];
      }
   }
}

int main(int argc, char* argv[]) {
   int TC;
   scanf("%d", &TC);
   FOR(tc, 1, TC) {
      scanf("%d %d", &N, &K);
      adj = vector< vector<Edge> >(N+1);
      REP(j, N-1) {
         int u, v, w;
         scanf("%d %d %d", &u, &v, &w);
         adj[u].push_back({v, w});
         adj[v].push_back({u, w});
      }

      DP_sat = VL(N+1);
      DP_notsat = VL(N+1);
      dfs(1);

      //FOR(u, 1, N)
      // fprintf(stderr, "%d: %lld %lld\n", u, DP_notsat[u], DP_sat[u]);

      llong res = max(DP_sat[1], DP_notsat[1]);
      printf("%lld\n", res);
   }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back

const int MX=5e5;
ll q;
ll n, k, U, V, W;
vii adjList[MX];
vii children[MX];
ll mem[MX][2];

void dfs(ll u, ll p) {
    for(ii v : adjList[u]) {
        if(v.first == p) continue;
        children[u].pb(v);
        dfs(v.first, u);
    }
}
ll getAns(ll u, bool c) {
    if(mem[u][c] == -1) {
        mem[u][c] = 0;
        vii bestOptions;
        for(ii v:children[u]) {
            ll connect   = getAns(v.first,1)+v.second;
            ll noConnect = getAns(v.first,0);
            bestOptions.pb({connect-noConnect,noConnect});
        }
        sort(bestOptions.begin(), bestOptions.end(), greater<ii>());
        int choosen = k-c;
        RE(i,bestOptions.size()) {
            if(i < choosen) mem[u][c] += max(bestOptions[i].second, bestOptions[i].second+bestOptions[i].first);
            else mem[u][c] += bestOptions[i].second;
        }
    }
    return mem[u][c];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>q;
    while(q--) {
        cin>>n>>k;
        RE(i,n) adjList[i].clear(), children[i].clear();
        RE(i,n-1) cin>>U>>V>>W,V--,U--, adjList[U].pb({V,W}), adjList[V].pb({U,W});
        dfs(0,-1);
        RE(i,n) mem[i][0]=mem[i][1]=-1;
        cout<<getAns(0,0)<<endl;
    }
}


/*   Abhijith  */
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b)	for(lld i=a;i<=b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define	endl '\n'
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
lld x,y,n,k,z;
vector<pair<lld,lld> >  adj[500005];
vector<lld> dp1(500005,0),dp2(500005,0);      //dp1 - k children
void dfs(lld s,lld par)
{
    lld sz=adj[s].size();
    vector<pair<lld,lld> > temp(sz);
    for(lld i=0;i<sz;i++)
    {
        if(adj[s][i].first==par) continue;
        dfs(adj[s][i].first,s);
        temp[i]={adj[s][i].second,adj[s][i].first};
    }
    sort(all(temp),greater<pair<lld,lld> >());
    for(lld i=0;i<min(k,sz);i++)
    {
        dp1[s]+=(temp[i].first+dp2[temp[i].second]);
        if(i!=k-1)
            dp2[s]+=(temp[i].first+dp2[temp[i].second]);
        else
            dp2[s]+=(dp1[temp[i].second]);
    }
    if(k<sz)
    {
        rep(i,k,sz-1)
        {
            dp2[s]+=(dp1[temp[i].second]);
            dp1[s]+=(dp1[temp[i].second]);
        }
    }
}
int main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     lld q;
     cin>>q;
     while(q--)
     {
          cin>>n>>k;
          rep(i,0,n-2)
          {
              cin>>x>>y>>z;
              adj[x].pb({y,z});
              adj[y].pb({x,z});  //if graph is directed delete this line
          }
          dfs(1LL,0LL);
          cout<<dp1[1]<<endl;
          rep(i,1,n)
          {
              adj[i].clear();
              dp1[i]=dp2[i]=0;
          }
     }
    return 0;
}


/*   Abhijith  */
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b)	for(lld i=a;i<=b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define	endl '\n'
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
lld x,y,n,k,z,ans=0;
vector<pair<lld,lld> >  adj[500005];
vector<lld> dp1(500005,0),dp2(500005,0);      //dp1 - k children
void dfs(lld s,lld par)
{
    lld sz=adj[s].size();
    vector<lld> temp(sz);
    lld hi=0;
    for(lld i=0;i<sz;i++)
    {
        if(adj[s][i].first==par) continue;
        dfs(adj[s][i].first,s);
        temp[i]=dp2[adj[s][i].first]+adj[s][i].second-dp1[adj[s][i].first];
        hi+=dp1[adj[s][i].first];
    }
    sort(all(temp),greater<lld>());
    dp1[s]=dp2[s]=hi;
    for(lld i=0;i<min(k,sz);i++)
    {
        if(temp[i]<=0)
            break;
        dp1[s]+=temp[i];
        if(i!=k-1)
            dp2[s]+=temp[i];
    }
}
int main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     lld q;
     cin>>q;
     while(q--)
     {
          cin>>n>>k;
          rep(i,0,n-2)
          {
              cin>>x>>y>>z;
              adj[x].pb({y,z});
              adj[y].pb({x,z});  //if graph is directed delete this line
          }
          dfs(1LL,0LL);
          cout<<dp1[1]<<endl;
          ans=0;
          rep(i,1,n)
          {
              adj[i].clear();
              dp1[i]=dp2[i]=0;
          }
     }
    return 0;
}


/*   Abhijith  */
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b)	for(lld i=a;i<=b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define	endl '\n'
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
lld x,y,n,k,z,ans=0;
vector<pair<lld,lld> >  adj[500005];
vector<lld> dp1(500005,0),dp2(500005,0);      //dp1 - k children
void dfs(lld s,lld par)
{
    lld sz=adj[s].size();
    vector<pair<lld,lld> > temp(sz);
    for(lld i=0;i<sz;i++)
    {
        if(adj[s][i].first==par) continue;
        dfs(adj[s][i].first,s);
        temp[i]={adj[s][i].second,adj[s][i].first};
    }
    sort(all(temp),greater<pair<lld,lld> >());
    for(lld i=0;i<min(k,sz);i++)
    {
        dp1[s]+=(temp[i].first+dp2[temp[i].second]);
        if(i!=k-1)
            dp2[s]+=(temp[i].first+dp2[temp[i].second]);
        else
            dp2[s]+=(dp1[temp[i].second]);
    }
    if(k<sz)
    {
        rep(i,k,sz-1)
        {
            dp2[s]+=(dp1[temp[i].second]);
            dp1[s]+=(dp1[temp[i].second]);
        }
    }
    ans=max(ans,dp1[s]);
}
int main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     lld q;
     cin>>q;
     while(q--)
     {
          cin>>n>>k;
          rep(i,0,n-2)
          {
              cin>>x>>y>>z;
              adj[x].pb({y,z});
              adj[y].pb({x,z});  //if graph is directed delete this line
          }
          dfs(1LL,0LL);
          cout<<ans<<endl;
          ans=0;
          rep(i,1,n)
          {
              adj[i].clear();
              dp1[i]=dp2[i]=0;
          }
     }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAX_N = 5e5 + 10;

struct Edge {
	int to;
	int weight;
	Edge() {}
	Edge(int to, int weight): to(to), weight(weight) {}
	Edge(const Edge &temp): to(temp.to), weight(temp.weight) {}
};

int n_vertex;
vector<Edge> edge[MAX_N];
int n_color;
int par[MAX_N];
long long ans;

void init() {
	for(int i=1; i<=n_vertex; ++i) {
		par[i] = -1;
		edge[i].clear();
	}
	ans = 0;
}

bool cmp(int a, int b) {
	return a > b;
}

int dfs(int now, int pre_weight) {
	vector<int> diff;
	for(unsigned int i=0; i<edge[now].size(); ++i) {
		int to = edge[now][i].to;
		int weight = edge[now][i].weight;
		if(to != par[now]) {
			par[to] = now;
			int temp = dfs(to, weight);
			if(temp > 0) diff.push_back(temp);
		}
	}
//	cout << now << ' ' << ans << endl;
	if(now == 1) {
		sort(diff.begin(), diff.end(), cmp);
		for(int i=0; i<n_color&&i<diff.size(); ++i) ans += diff[i];
		return 0;
	} else if(diff.size() < n_color) {
		for(unsigned int i=0; i<diff.size(); ++i) ans += diff[i];
		return pre_weight;
	} else {
		sort(diff.begin(), diff.end(), cmp);
		for(int i=0; i<n_color; ++i) ans += diff[i];
		return pre_weight - diff[n_color-1];
	}
}

int main() {
	int n_query;
	scanf("%d", &n_query);
	while(n_query--) {
		scanf("%d %d", &n_vertex, &n_color);
		init();
		for(int i=0; i<n_vertex-1; ++i) {
			int u, v, weight;
			scanf("%d %d %d", &u, &v, &weight);
			edge[u].push_back(Edge(v, weight));
			edge[v].push_back(Edge(u, weight));
		}
		dfs(1, 0);
		printf("%I64d\n", ans);
	}
    return 0;
}
/**
*/


#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FOREACH(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define SIZE(v) ((int)(v).size())
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define ll long long
#define pii pair<int, int>

const int MAX = 500500;
vector<pair<int, ll> > adj[MAX];
int n, k;
ll dp[MAX][2];
bool visited[MAX][2];

bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
    return a.nd - a.st > b.nd - b.st;
}

ll solve(int u, int p, int used) {
    if(!visited[u][used]) {
        visited[u][used] = true;
        dp[u][used] = 0;
        int nCol = -1;
        if(used == 0) nCol = k;
        else nCol = k - 1;
        vector<pair<ll, ll> > childrenValues;
        FOREACH(v, adj[u]) if(v->first != p) {
            childrenValues.pb(mp( solve(v->first, u, 0) , solve(v->first, u, 1) + v->second ));
        }
        sort(childrenValues.begin(), childrenValues.end(), compare);
        //printf("u = %d, used = %d\n", u, used);
        int cnt = 0, len = (int)childrenValues.size();
        //REP(i, len) printf("<%lld, %lld>%c", childrenValues[i].st, childrenValues[i].nd, i == len - 1 ? '\n' : ' ');

        //printf("nCol = %d, len = %d\n", nCol, len);
        for(int i = 0; i < nCol && i < len; i++) if(childrenValues[i].nd - childrenValues[i].st > 0) {
            dp[u][used] += childrenValues[cnt].nd;
            cnt++;
        }
        for(int i = cnt; i < len; i++) dp[u][used] += childrenValues[i].st;
        //printf("u = %d, p = %d, used = %d, dp = %lld\n", u, p, used, dp[u][used]);
    }
    return dp[u][used];
}

int main() {
    int Q; scanf("%d", &Q);
    REP(q, Q) {
        scanf("%d %d", &n, &k);
        REP(i, n) adj[i].clear(), visited[i][0] = visited[i][1] = false;
        REP(i, n - 1) {
            int u, v;
            ll w; scanf("%d %d %lld", &u, &v, &w); u--, v--;
            adj[u].pb(mp(v, w));
            adj[v].pb(mp(u, w));
        }
        printf("%lld\n", solve(0, -1, 0));
    }
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FOREACH(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define SIZE(v) ((int)(v).size())
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define ll long long
#define pii pair<int, int>

const int MAX = 500500;
vector<pair<int, ll> > adj[MAX];
int n, k;
ll dp[MAX][2];
bool visited[MAX][2];

bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
    return a.nd - a.st > b.nd - b.st;
}

ll solve(int u, int p, int used) {
    if(!visited[u][used]) {
        dp[u][used] = 0;
        int nCol = -1;
        if(used == 0) nCol = k;
        else nCol = k - 1;
        vector<pair<ll, ll> > childrenValues;
        FOREACH(v, adj[u]) if(v->first != p) {
            childrenValues.pb(mp( solve(v->first, u, 0) , solve(v->first, u, 1) + v->second ));
        }
        sort(childrenValues.begin(), childrenValues.end(), compare);
        //printf("u = %d, used = %d\n", u, used);
        int cnt = 0, len = (int)childrenValues.size();
        //REP(i, len) printf("<%lld, %lld>%c", childrenValues[i].st, childrenValues[i].nd, i == len - 1 ? '\n' : ' ');

        //printf("nCol = %d, len = %d\n", nCol, len);
        for(int i = 0; i < nCol && i < len; i++) if(childrenValues[i].nd - childrenValues[i].st > 0) {
            dp[u][used] += childrenValues[cnt].nd;
            cnt++;
        }
        for(int i = cnt; i < len; i++) dp[u][used] += childrenValues[i].st;
        //printf("u = %d, p = %d, used = %d, dp = %lld\n", u, p, used, dp[u][used]);
    }
    return dp[u][used];
}

int main() {
    int Q; scanf("%d", &Q);
    REP(q, Q) {
        scanf("%d %d", &n, &k);
        REP(i, n) adj[i].clear(), visited[i][0] = visited[i][1] = false;
        REP(i, n - 1) {
            int u, v;
            ll w; scanf("%d %d %lld", &u, &v, &w); u--, v--;
            adj[u].pb(mp(v, w));
            adj[v].pb(mp(u, w));
        }
        printf("%lld\n", solve(0, -1, 0));
    }
}


/*
数据不清空，爆零两行泪。
多测不读完，爆零两行泪。
边界不特判，爆零两行泪。
贪心不证明，爆零两行泪。
D P 顺序错，爆零两行泪。
大小少等号，爆零两行泪。
变量不统一，爆零两行泪。
越界不判断，爆零两行泪。
调试不注释，爆零两行泪。
溢出不 l l，爆零两行泪。
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define X first
#define Y second
int n,m;
const int N=500000;
vector<pair<int,int> > nei[N+1];
int f[N+1][2];
bool vis[N+1][2];
bool cmp(pair<int,int> x,pair<int,int> y){return x.X-x.Y>y.X-y.Y;}
int dp(int x=1,bool c=false,int fa=0){
	if(vis[x][c])return f[x][c];
	vis[x][c]=true;
	vector<pair<int,int> > v;
	for(int i=0;i<nei[x].size();i++){
		int y=nei[x][i].X,len=nei[x][i].Y;
		if(y==fa)continue;
		v.pb(mp(dp(y,true,x)+len,dp(y,false,x)));
	}
	sort(v.begin(),v.end(),cmp);
	for(int i=0;i<v.size();i++)
		if(i>=m-c||v[i].Y>v[i].X)f[x][c]+=v[i].Y;
		else f[x][c]+=v[i].X;
//	printf("f[%lld][%d]=%lld\n",x,c,f[x][c]);
	return f[x][c];
}
void mian(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)nei[i].clear();
	for(int i=1;i<n;i++){
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		nei[x].pb(mp(y,z));nei[y].pb(mp(x,z));
	}
	for(int i=1;i<=n;i++)f[i][0]=f[i][1]=0;
	cout<<dp()<<"\n";
}
signed main(){
	int testnum;
	cin>>testnum;
	while(testnum--)mian();
	return 0;
}
/*1
1
9 3
7 4 10
6 8 1
1 3 6
2 4 10
2 9 1
5 4 9
3 6 5
1 2 5
*/


/*
数据不清空，爆零两行泪。
多测不读完，爆零两行泪。
边界不特判，爆零两行泪。
贪心不证明，爆零两行泪。
D P 顺序错，爆零两行泪。
大小少等号，爆零两行泪。
变量不统一，爆零两行泪。
越界不判断，爆零两行泪。
调试不注释，爆零两行泪。
溢出不 l l，爆零两行泪。
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define X first
#define Y second
int n,m;
const int N=500000;
vector<pair<int,int> > nei[N+1];
int f[N+1][2];
bool cmp(pair<int,int> x,pair<int,int> y){return x.X-x.Y>y.X-y.Y;}
int dp(int x=1,bool c=false,int fa=0){
	vector<pair<int,int> > v;
	for(int i=0;i<nei[x].size();i++){
		int y=nei[x][i].X,len=nei[x][i].Y;
		if(y==fa)continue;
		v.pb(mp(dp(y,true,x)+len,dp(y,false,x)));
	}
	sort(v.begin(),v.end(),cmp);
	for(int i=0;i<v.size();i++)
		if(i>=m-c||v[i].Y>v[i].X)f[x][c]+=v[i].Y;
		else f[x][c]+=v[i].X;
	return f[x][c];
}
void mian(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)nei[i].clear();
	for(int i=1;i<n;i++){
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		nei[x].pb(mp(y,z));nei[y].pb(mp(x,z));
	}
	for(int i=1;i<=n;i++)f[i][0]=f[i][1]=0;
	cout<<dp()<<"\n";
}
signed main(){
	int testnum;
	cin>>testnum;
	while(testnum--)mian();
	return 0;
}


/*
数据不清空，爆零两行泪。
多测不读完，爆零两行泪。
边界不特判，爆零两行泪。
贪心不证明，爆零两行泪。
D P 顺序错，爆零两行泪。
大小少等号，爆零两行泪。
变量不统一，爆零两行泪。
越界不判断，爆零两行泪。
调试不注释，爆零两行泪。
溢出不 l l，爆零两行泪。
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define X first
#define Y second
int n,m;
const int N=500000;
vector<pair<int,int> > nei[N+1];
int f[N+1][2];
bool vis[N+1][2];
bool cmp(pair<int,int> x,pair<int,int> y){return x.X-x.Y>y.X-y.Y;}
int dp(int x=1,bool c=false,int fa=0){
	if(vis[x][c])return f[x][c];
	vis[x][c]=true;
	vector<pair<int,int> > v;
	for(int i=0;i<nei[x].size();i++){
		int y=nei[x][i].X,len=nei[x][i].Y;
		if(y==fa)continue;
		v.pb(mp(dp(y,true,x)+len,dp(y,false,x)));
	}
	sort(v.begin(),v.end(),cmp);
	for(int i=0;i<v.size();i++)
		if(i>=m-c||v[i].Y>v[i].X)f[x][c]+=v[i].Y;
		else f[x][c]+=v[i].X;
//	printf("f[%lld][%d]=%lld\n",x,c,f[x][c]);
	return f[x][c];
}
void mian(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)nei[i].clear();
	for(int i=1;i<n;i++){
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		nei[x].pb(mp(y,z));nei[y].pb(mp(x,z));
	}
	for(int i=1;i<=n;i++)vis[i][0]=vis[i][1]=f[i][0]=f[i][1]=0;
	cout<<dp()<<"\n";
}
signed main(){
	int testnum;
	cin>>testnum;
	while(testnum--)mian();
	return 0;
}
/*1
1
9 3
7 4 10
6 8 1
1 3 6
2 4 10
2 9 1
5 4 9
3 6 5
1 2 5
*/


#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); ++i)

const int N = 500 * 1000 + 13;

int n, k;
vector<pair<int, int>> g[N];
long long dp[N][2];

void calc(int v, int p = -1) {
	long long cur = 0;
	vector<long long> adds;

	for (auto it : g[v]) {
		int to = it.x;
		int w = it.y;
		if (to == p)
			continue;
		calc(to, v);

		cur += dp[to][0];
		adds.pb(dp[to][1] + w - dp[to][0]);
	}

	sort(all(adds), greater<long long>());
	forn(i, min(sz(adds), k)) if (adds[i] > 0)
		cur += adds[i];

	dp[v][0] = dp[v][1] = cur;
	if (k <= sz(adds) && adds[k - 1] > 0)
		dp[v][1] -= adds[k - 1];
}

long long solve() {
	scanf("%d%d", &n, &k);
	forn(i, n) g[i].clear();
	forn(i, n - 1) {
		int x, y, w;
		scanf("%d%d%d", &x, &y, &w);
		--x;
		--y;
		g[x].pb(mp(y, w));
		g[y].pb(mp(x, w));
	}

	calc(0);

	return dp[0][0];
}

int main() {
	int q;
	scanf("%d", &q);
	forn(i, q) printf("%lld\n", solve());
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define For(i,n) for(int i=0;i<n;i++)
#define Fre(i,a,b) for(int i = a; i < b; i++)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pfn(n) printf("%d\n", n)
#define pfs(n) printf("%d ", n)
#define ff first
#define ss second
#define mem(a,b) memset(a,b,sizeof(a))
#define sz size()
#define number_of_digit(n) __builtin_ffs(n);
#define ll long long
#define T int
#define ld long double
inline void print(T n) {
	if (n == 0) {
		putchar('0');
		putchar('\n');
	} else if (n == -1) {
		putchar('-');
		putchar('1');
		putchar('\n');
	} else {
		char buf[11];
		buf[10] = '\n';
		int i = 9;
		while (n) {
			buf[i--] = n % 10 + '0';
			n /= 10;
		}
		while (buf[i] != '\n')
			putchar(buf[++i]);
	}
}
int read() {
	int cc = getc(stdin);
	for (; cc < '0' || cc > '9';)  cc = getc(stdin);
	int ret = 0;
	for (; cc >= '0' && cc <= '9';) {
		ret = ret * 10 + cc - '0';
		cc = getc(stdin);
	}
	return ret;
}
ll power(ll num,ll g) {
	if(g==0)return 1;

	if(g%2==1)return (num*power((num*num),g/2));
	return power((num*num),g/2);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		ll p[n+1];
		For(i,n)cin>>p[i+1];
		ll x,a,y,b;
		cin>>x>>a>>y>>b;
		ll k;
		cin>>k;
		if(x<y) {
			swap(a,b);
			swap(x,y);
		}
		ll start=1,end=n;
		sort(p+1,p+n+1,greater<ll>());
		ll c=__gcd(a,b);
		k*=100;
		c=a*b/c;
		// cout<<c<<endl;
		ll g=n/c;
		ll f=n/a-g;
		ll h=n/b-g;
		ll sum=0;
		for(int i=1; i<=g; i++) {
			sum+=(p[i]*(x+y));
		}
		for(int i=g+1; i<=g+f; i++)sum+=p[i]*x;
		// cout<<sum<<endl;
		for(int i=f+g+1; i<=f+g+h; i++)sum+=p[i]*y;
		// cout<<sum<<endl;
		if(sum<k) {
			cout<<"-1\n";
			continue;
		}
		while(start<end) {
			int mid=(start+end)/2;
			n=mid;
			g=n/c;
			f=n/a-g;
			h=n/b-g;
			sum=0;
			for(int i=1; i<=g; i++) {
				sum+=(p[i]*(x+y));
			}
			for(int i=g+1; i<=g+f; i++)sum+=p[i]*x;
			for(int i=f+g+1; i<=f+g+h; i++)sum+=p[i]*y;
			if(sum>=k)end=mid;
			else start=mid+1;
		}
		cout<<start<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500015

using namespace std;
int q, n, a[N];
int k;
long long lcm;
int l[N], r[N];

long long mem[N][4];

vector<pair<long long, long long> > g[N];

long long dfs(int x, int p, int t) {
    if (mem[x][t] != -1)
        return mem[x][t];
    int ss = ((int) g[x].size());
    pair<long long, pair<long long, long long> > v[g[x].size()];

    memset(v, 0, sizeof (v));

    for (int i = 0; i < g[x].size(); i++) {
        if (g[x][i].first == p)
            continue;
        v[i].second.first = dfs(g[x][i].first, x, 1) + g[x][i].second;
        v[i].second.second = dfs(g[x][i].first, x, 0);
        v[i].first = v[i].second.first - v[i].second.second;
    }

    sort(v, v + ss, greater<pair<long long, pair<long long, long long> > >());
    long long kk = 0;
    long long kkm1 = 0;
    int tt = 0;
    for (int i = 0; i < ss; i++) {
        if (v[i].first <= 0) {
            kk += v[i].second.second;
            kkm1 += v[i].second.second;
            continue;
        }
        if (tt < k) {
            kk += v[i].second.first;
        } else {
            kk += v[i].second.second;
        }
        if (tt < k - 1) {
            kkm1 += v[i].second.first;
        } else {
            kkm1 += v[i].second.second;
        }
        tt++;
    }
    mem[x][0] = kk;
    mem[x][1] = kkm1;
    return mem[x][t];
}

int main() {

    scanf("%d", &q);
    while (q--) {
        scanf("%d%d", &n, &k);
        for (int i = 1; i <= n; i++) {
            mem[i][0] = mem[i][1] = -1;
        }
        for (int i = 0; i < n - 1; i++) {
            int x, y, w;
            scanf("%d%d%d", &x, &y, &w);
            g[x].push_back(make_pair(y, w));
            g[y].push_back(make_pair(x, w));
        }
        printf("%lld\n", dfs(1, -1, 0));
        for (int i = 1; i <= n; i++) {
            g[i].clear();
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

void solve(){
    int n,k;cin>>n>>k;
    vi d;
    vector<pair<ll, pii> > e;
    d.resize(n+1,0);
    rep(i,n-1){
        int u,v,w;cin>>u>>v>>w;
        e.pb({w,{u,v}});
    }
    sort(all(e));
    reverse(all(e));

    ll sum=0;
    trav(it,e){
        ll w=it.fst;
        int u=it.snd.fst, v=it.snd.snd;
        if(d[u]<k && d[v]<k) sum+=w, d[u]++, d[v]++;
    }
    cout<<sum<<"\n";
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    int t;cin>>t;
    rep(i,t)solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

vector<pair<int, ll> > g[500010];
vi vis;
vector<ll> dp1, dp2;

void solve2(int u, int h, int k){
    vis[u]=1;
    trav(p,g[u]){
        if(p.fst!=h)solve2(p.fst, u, k);
    }
    vector<pair<ll, ll> > a;
    trav(p,g[u])if(p.fst!=h){
        int v=p.fst;ll w=p.snd;
        a.pb({dp2[v]+w-dp1[v], dp1[v]});
    }
    sort(all(a));
    reverse(all(a));
    rep(i,sz(a)){
        if(i<k && a[i].fst>0) dp1[u]+=a[i].fst+a[i].snd;
        else dp1[u]+=a[i].snd;
    }
    rep(i,sz(a)){
        if(i<k-1 && a[i].fst>0) dp2[u]+=a[i].fst+a[i].snd;
        else dp2[u]+=a[i].snd;
    }
}

void solve(){
    int n,k;cin>>n>>k;
    rep(i,n)g[i].clear();
    rep(i,n-1){
        int u,v,w;cin>>u>>v>>w;
        u--,v--;
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    vis.clear();
    dp1.clear();
    dp2.clear();
    vis.resize(n+10,0);
    dp1.resize(n+10,0);
    dp2.resize(n+10,0);
    solve2(0,-1,k);
    cout << max(dp1[0], dp2[0]) << '\n';
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    int t;cin>>t;
    rep(i,t)solve();

	return 0;
}


#include <bits/stdc++.h>
//#include <queue>
//#include <cmath>
//#include <iostream>
//#include <unordered_map>

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) ((x)&(-x))
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define INF 0x3f3f3f3f
#define ll long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int mod=1e9+7;
const int N=3e5+9;

vector<pair<int,ll> >g[N];
ll dp[N][2];
int n,k;

void dfs(int u,int fa)
{
    dp[u][1]=dp[u][0]=0;
    ll sum=0;
    vector<ll>vec;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i].first;
        ll w=g[u][i].second;
        if(v==fa) continue;
        dfs(v,u);
        sum+=dp[v][0];
        vec.pb(dp[v][1]+w-dp[v][0]);
    }
    sort(vec.begin(),vec.end(),greater<int>());
    for(int i=0;i<min(k,(int)vec.size());i++)
    {
        if(vec[i]>0)
            sum+=vec[i];
    }
    dp[u][0]=dp[u][1]=sum;
    if(vec.size()>=k) dp[u][1]-=vec[k-1];
}

int main()
{
    FAST_IO;

    int q;
    cin>>q;
    while(q--)
    {
        cin>>n>>k;
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int u,v;
            ll w;
            cin>>u>>v>>w;
            g[u].pb({v,w});
            g[v].pb({u,w});

        }

        dfs(1,0);

        cout<<max(dp[1][1],dp[1][0])<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
//#include <queue>
//#include <cmath>
//#include <iostream>
//#include <unordered_map>

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) ((x)&(-x))
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define INF 0x3f3f3f3f
#define ll long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int mod=1e9+7;
const int N=5e5+9;

vector<pair<int,ll> >g[N];
ll dp[N][2];
int n,k;

void dfs(int u,int fa)
{
    dp[u][1]=dp[u][0]=0;
    ll sum=0;
    vector<ll>vec;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i].first;
        ll w=g[u][i].second;
        if(v==fa) continue;
        dfs(v,u);
        sum+=dp[v][0];
        vec.pb(dp[v][1]+w-dp[v][0]);
    }
    sort(vec.begin(),vec.end(),greater<int>());
    for(int i=0;i<min(k,(int)vec.size());i++)
    {
        if(vec[i]>0)
            sum+=vec[i];
    }
    dp[u][0]=dp[u][1]=sum;
    if(vec.size()>=k) if(vec[k-1]>0) dp[u][1]-=vec[k-1];
}

int main()
{
    FAST_IO;

    int q;
    cin>>q;
    while(q--)
    {
        cin>>n>>k;
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int u,v;
            ll w;
            cin>>u>>v>>w;
            g[u].pb({v,w});
            g[v].pb({u,w});

        }

        dfs(1,0);

        cout<<max(dp[1][1],dp[1][0])<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
//#include <queue>
//#include <cmath>
//#include <iostream>
//#include <unordered_map>

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) ((x)&(-x))
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define INF 0x3f3f3f3f
#define ll long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int mod=1e9+7;
const int N=3e5+9;

vector<pair<int,ll> >g[N];
ll dp[N][2];
int n,k;

void dfs(int u,int fa)
{
    dp[u][1]=dp[u][0]=0;
    ll sum=0;
    vector<ll>vec;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i].first;
        ll w=g[u][i].second;
        if(v==fa) continue;
        dfs(v,u);
        sum+=dp[v][0];
        vec.pb(dp[v][1]+w-dp[v][0]);
    }
    sort(vec.begin(),vec.end(),greater<int>());
    for(int i=0;i<min(k,(int)vec.size());i++)
    {
        if(vec[i]>0)
            sum+=vec[i];
    }
    dp[u][0]=dp[u][1]=sum;
    if(vec.size()>=k) if(vec[k-1]>0) dp[u][1]-=vec[k-1];
}

int main()
{
    FAST_IO;

    int q;
    cin>>q;
    while(q--)
    {
        cin>>n>>k;
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int u,v;
            ll w;
            cin>>u>>v>>w;
            g[u].pb({v,w});
            g[v].pb({u,w});

        }

        dfs(1,0);

        cout<<max(dp[1][1],dp[1][0])<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i=0; i<int(n); i++)
#define RNG(i, f, t) for(int i=int(f); i<=int(t); i++)
bool RELAXED = false;
template<int,typename T> struct MINMAX{ T val; MINMAX(T val) : val(val) {} };
template<typename T> MINMAX<1,T> MAX(T val) { return MINMAX<1,T>(val); };
template<typename T> MINMAX<2,T> MIN(T val) { return MINMAX<2,T>(val); };
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<1,U> rhs) { return lhs = (rhs.val > lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<2,U> rhs) { return lhs = (rhs.val < lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T> inline vector<T> readVector(int n) { vector<T> vec(n); FOR(i,n) cin>>vec[i]; return vec; }
template<typename T> void EXIT(T answer) { cout<<answer<<endl; exit(0); }
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef signed long long int ll;

const int MAXN = 5e5+5;
vector<vpii> adj(MAXN);
vector<vector<ll>> dp(MAXN, vector<ll>(2));
int n,k;

ll dfs(int u, int p, int kk) {
    if(dp[u][k-kk] >= 0) return dp[u][k-kk];

    ll sum=0;
    vector<ll> extra;
    for(auto to : adj[u]) {
        if(to.first==p) continue;
        ll nosat = dfs(to.first, u, k);
        ll sat = dfs(to.first, u, k-1) + to.second;
        sum += nosat;
        extra.push_back(sat - nosat);
    }
    sort(extra.rbegin(), extra.rend());
    FOR(i,min(kk,(int)extra.size())) {
        if(extra[i] > 0)
            sum += extra[i];
    }

    return dp[u][k-kk] = sum;
}

void solve() {
    for(auto &e : adj) e.clear();
    for(auto &e : dp) e = {-1LL, -1LL};

    cin>>n>>k;
    FOR(i,n-1) {
        int a,b,w;
        cin>>a>>b>>w;
        adj[a].push_back({b,w});
        adj[b].push_back({a,w});
    }

    ll ans = dfs(1,-1,k);
    cout<<ans<<endl;
}

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

    int T; cin>>T;
    while(T--) solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i=0; i<int(n); i++)
#define RNG(i, f, t) for(int i=int(f); i<=int(t); i++)
bool RELAXED = false;
template<int,typename T> struct MINMAX{ T val; MINMAX(T val) : val(val) {} };
template<typename T> MINMAX<1,T> MAX(T val) { return MINMAX<1,T>(val); };
template<typename T> MINMAX<2,T> MIN(T val) { return MINMAX<2,T>(val); };
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<1,U> rhs) { return lhs = (rhs.val > lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<2,U> rhs) { return lhs = (rhs.val < lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T> inline vector<T> readVector(int n) { vector<T> vec(n); FOR(i,n) cin>>vec[i]; return vec; }
template<typename T> void EXIT(T answer) { cout<<answer<<endl; exit(0); }
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef signed long long int ll;

const int MAXN = 5e5+5;
vector<vpii> adj(MAXN);
int n,k;

ll dfs(int u, int p, int kk) {
    ll sum=0;
    vector<ll> extra;
    for(auto to : adj[u]) {
        if(to.first==p) continue;
        ll nosat = dfs(to.first, u, k);
        ll sat = dfs(to.first, u, k-1) + to.second;
        sum += nosat;
        extra.push_back(sat - nosat);
    }
    sort(extra.rbegin(), extra.rend());
    FOR(i,min(kk,(int)extra.size())) {
        if(extra[i] > 0)
            sum += extra[i];
    }

    return sum;
}

void solve() {
    for(auto &e : adj) e.clear();

    cin>>n>>k;
    FOR(i,n-1) {
        int a,b,w;
        cin>>a>>b>>w;
        adj[a].push_back({b,w});
        adj[b].push_back({a,w});
    }

    ll ans = dfs(1,-1,k);
    cout<<ans<<endl;
}

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

    int T; cin>>T;
    while(T--) solve();
    return 0;
}