#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long q;
  cin >> q;
  for (long long t = 0; t < q; t++) {
    long long n, k;
    cin >> n >> k;
    vector<vector<long long>> adj((size_t)n);
    map<pair<long long, long long>, long long> weight;
    for (long long i = 0; i < n - 1; i++) {
      long long u, v, w;
      cin >> u >> v >> w;
      u--, v--;
      adj[u].push_back(v);
      adj[v].push_back(u);
      weight[{u, v}] = w;
      weight[{v, u}] = w;
    }
    vector<vector<long long>> dp((size_t)n, vector<long long>((size_t)2));
    function<void(long long, long long)> visit = [&](long long u, long long v) {
      dp[v][0] = 0;
      dp[v][1] = 0;
      vector<long long> g;
      for (long long w : adj[v]) {
        if (w != u) {
          visit(v, w);
          dp[v][0] += dp[w][1];
          dp[v][1] += dp[w][1];
          g.push_back(max(dp[w][0] + weight[{v, w}], dp[w][1]) - dp[w][1]);
        }
      }
      sort(g.rbegin(), g.rend());
      dp[v][0] += accumulate(
          g.begin(), g.begin() + min((long long)g.size(), k - 1), (long long)0);
      dp[v][1] += accumulate(g.begin(), g.begin() + min((long long)g.size(), k),
                             (long long)0);
    };
    visit(-1, 0);
    cout << dp[0][1] << endl;
  }
  return 0;
}
