#include <bits/stdc++.h>
using namespace std;
long long int n, k, val[500005][2];
std::vector<std::vector<pair<long long int, long long int> > > adj(500005);
void dfs(long long int x, long long int p) {
  long long int cur = 0;
  vector<long long int> va;
  for (auto it : adj[x]) {
    if (it.first == p) {
      continue;
    }
    dfs(it.first, x);
    cur += val[it.first][0];
    va.push_back(val[it.first][1] + it.second - val[it.first][0]);
  }
  sort(va.begin(), va.end(), greater<long long int>());
  long long int tem = va.size();
  for (long long int i = 0; i < min(tem, k); i++) {
    if (va[i] > 0) {
      cur += va[i];
    }
  }
  val[x][0] = val[x][1] = cur;
  if (k <= va.size() && va[k - 1] > 0) {
    val[x][1] -= va[k - 1];
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int i, j, x, y, w;
    cin >> n >> k;
    for (i = 1; i < n; i++) {
      val[i][0] = val[i][1] = 0;
    }
    for (i = 1; i < n; i++) {
      cin >> x >> y >> w;
      adj[x].push_back(make_pair(y, w));
      adj[y].push_back(make_pair(x, w));
    }
    dfs(1, 0);
    cout << val[1][0] << "\n";
    for (i = 1; i <= n; i++) {
      adj[i].clear();
    }
  }
  return 0;
}
