#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 5;
long long n, k;
long long dp[N][2];
vector<pair<long long, long long> > g[N];
void dfs(long long u, long long par) {
  dp[u][0] = 0, dp[u][1] = 0;
  long long sum = 0;
  vector<long long> v;
  for (auto &it : g[u]) {
    if (it.first == par) continue;
    dfs(it.first, u);
    sum += dp[it.first][0];
    long long gain = dp[it.first][1] + it.second - dp[it.first][0];
    if (gain > 0) v.push_back(gain);
  }
  sort(v.rbegin(), v.rend());
  long long sz = min(k - 1, (long long)v.size());
  for (long long i = 0; i < sz; i++) sum += v[i];
  dp[u][1] = sum;
  dp[u][0] = sum;
  if (sz < v.size()) dp[u][0] += v[sz];
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (long long i = 1; i <= n; i++) g[i].clear();
    for (long long i = 1; i <= n - 1; i++) {
      long long u, v, w;
      cin >> u >> v >> w;
      g[u].push_back({v, w});
      g[v].push_back({u, w});
    }
    dfs(1, 0);
    cout << dp[1][0] << "\n";
  }
  return 0;
}
