#include <bits/stdc++.h>
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
long long MOD = 998244353;
long long ans = 0;
vector<vector<pair<long long, long long>>> G;
long long dfs(int x, int p, int k) {
  long long lost = 0;
  vector<long long> v;
  for (auto e : G[x]) {
    if (e.first == p) continue;
    long long t = e.second - dfs(e.first, x, k);
    if (t > 0) v.push_back(t);
  }
  if (v.size() < k) {
    for (auto e : v) ans += e;
    return 0;
  } else {
    sort(begin(v), end(v), greater<long long>());
    for (long long i = 0; i < k; i++) {
      ans += v[i];
    }
    return v[k - 1];
  }
}
int main() {
  int q = in();
  for (long long iii = 0; iii < q; iii++) {
    int n = in();
    int k = in();
    G = vector<vector<pair<long long, long long>>>(n + 1);
    ans = 0;
    for (long long i = 0; i < n - 1; i++) {
      int a = in(), b = in();
      long long c = lin();
      G[a].push_back({b, c});
      G[b].push_back({a, c});
    }
    dfs(1, 0, k);
    cout << ans << "\n";
  }
}
