#include <bits/stdc++.h>
using namespace std;
const int dydis = 5e5 + 10;
int n, k;
vector<pair<int, int> > gr[dydis];
long long dp[dydis][2];
long long f(int v, int took, int came) {
  if (dp[v][took] != -1) return dp[v][took];
  long long ret = 0;
  vector<pair<long long, long long> > vec;
  for (auto x : gr[v]) {
    if (x.first == came) continue;
    long long imu = f(x.first, 1, v) + x.second;
    long long neimu = f(x.first, 0, v);
    vec.push_back({imu - neimu, neimu});
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  for (int i = 0; i < vec.size(); i++) {
    ret += max(vec[i].second, 0ll);
    if (i < k - took) ret += max(vec[i].first, 0ll);
  }
  return dp[v][took] = ret;
}
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      gr[i].clear();
      dp[i][0] = dp[i][1] = -1;
    }
    for (int i = 0; i < n - 1; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      gr[a].push_back({b, c});
      gr[b].push_back({a, c});
    }
    cout << f(0, 0, -1) << "\n";
  }
  return 0;
}
