#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
int n, k;
vector<pair<long long, long long> > e[500005];
long long dp[500005][2];
void dfs(int cn, int p) {
  long long sm = 0;
  vector<long long> imp;
  long long up = 0;
  for (int i = 0; i < (signed)(e[cn].size()); i++) {
    int nn = e[cn][i].first;
    long long sc = e[cn][i].second;
    if (nn == p) {
      up = sc;
      continue;
    }
    dfs(nn, cn);
    sm += dp[nn][0];
    imp.push_back(dp[nn][1]);
  }
  sort(imp.begin(), imp.end(), greater<long long>());
  for (int i = 0; i < (signed)(min(k, (int)imp.size())); i++) {
    sm += imp[i];
  }
  if (imp.size() < k) {
    dp[cn][1] = up;
  } else {
    dp[cn][1] = max(0LL, up - imp[k - 1]);
  }
  dp[cn][0] = sm;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  for (int _ = 0; _ < (signed)(q); _++) {
    cin >> n >> k;
    for (int _ = 0; _ < (signed)(n - 1); _++) {
      int u, v, w;
      cin >> u >> v >> w;
      e[u].push_back({v, w});
      e[v].push_back({u, w});
    }
    dfs(1, 0);
    cout << dp[1][0] << "\n";
    for (int i = (1); i <= (signed)(n); i++) {
      e[i].clear();
      dp[i][0] = 0;
      dp[i][1] = 0;
    }
  }
  return 0;
}
