#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 10;
const long long inf = 1e18;
long long dp[maxn][2];
vector<pair<long long, long long> > g[maxn];
long long n, k;
void dfs(long long x, long long fa) {
  dp[x][0] = dp[x][1] = 0;
  vector<long long> tmp;
  for (long long i = 0; i < g[x].size(); i++) {
    long long v = g[x][i].first;
    if (v == fa) continue;
    dfs(v, x);
    dp[x][0] += dp[v][0];
    dp[x][1] += dp[v][0];
    tmp.push_back(g[x][i].second + dp[v][1] - dp[v][0]);
  }
  sort(tmp.rbegin(), tmp.rend());
  for (long long i = 0; i < k && i < tmp.size(); i++) {
    if (tmp[i] < 0) break;
    dp[x][1] += (i < k - 1 ? tmp[i] : 0);
    dp[x][0] += tmp[i];
  }
}
void solve() {
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) dp[i][0] = dp[i][1] = 0, g[i].clear();
  for (long long i = 1; i < n; 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';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
