#include <bits/stdc++.h>
using namespace std;
const long long N = 1000001;
vector<vector<pair<long long, long long>>> g(N);
vector<vector<long long>> dp(N, vector<long long>(2, 0));
long long n, k;
void dfs(long long u, long long p) {
  long long curr = 0;
  vector<long long> adds;
  for (auto it : g[u]) {
    long long v = it.first;
    long long w = it.second;
    if (v != p) {
      dfs(v, u);
      curr += dp[v][0];
      adds.push_back(dp[v][1] + w - dp[v][0]);
    }
  }
  sort(adds.begin(), adds.end(), greater<long long>());
  for (long long i = 0; i < min((long long)(adds.size()), k); ++i)
    if (adds[i] > 0) curr += adds[i];
  dp[u][0] = dp[u][1] = curr;
  if (k <= (long long)(adds.size()) && adds[k - 1] > 0) dp[u][1] -= adds[k - 1];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (long long i = 0; i < n - 1; ++i) {
      long long u, v, w;
      cin >> u >> v >> w;
      --u, --v;
      g[u].push_back({v, w});
      g[v].push_back({u, w});
    }
    dfs(0, -1);
    cout << dp[0][0] << "\n";
    for (long long i = 0; i < n; ++i) g[i].clear(), dp[i].clear();
  }
}
