#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 9;
long long n, k;
vector<vector<pair<long long, long long> > > g;
vector<long long> used;
vector<vector<long long> > dp;
void dfs(long long v) {
  used[v] = 1;
  long long cur = 0;
  vector<long long> can;
  for (auto ix : g[v]) {
    long long to = ix.first, len = ix.second;
    if (!used[to]) {
      dfs(to);
      can.push_back(dp[to][1] + len - dp[to][0]);
      cur += dp[to][0];
    }
  }
  sort(can.begin(), can.end(), greater<long long>());
  for (long long i = (long long)(0);
       i < (long long)(min((long long)(can.size()), k)); ++i)
    if (can[i] > 0) cur += can[i];
  dp[v][0] = dp[v][1] = cur;
  if (can.size() >= k && can[k - 1] > 0) dp[v][1] -= can[k - 1];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    g.clear();
    used.clear();
    dp.clear();
    cin >> n >> k;
    g.resize(n);
    used.resize(n, 0);
    dp.resize(n, vector<long long>(2, 0));
    for (long long i = (long long)(0); i < (long long)(n - 1); ++i) {
      long long u, v, cost;
      cin >> u >> v >> cost;
      --u, --v;
      g[u].push_back({v, cost});
      g[v].push_back({u, cost});
    }
    dfs(0);
    cout << dp[0][0] << '\n';
  }
  return 0;
}
