#include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <class T>
int size(T &&x) {
  return int(x.size());
}
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  out << '{';
  for (auto it = x.begin(); it != x.end(); ++it)
    out << *it << (it == prev(x.end()) ? "" : ", ");
  return out << '}';
}
void dump() {}
template <class T, class... Args>
void dump(T &&x, Args... args) {
  cerr << x << ";  ";
  dump(args...);
}
mt19937_64 rng(0);
int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
int n, k;
vector<vector<pair<int, int>>> graph;
vector<vector<LL>> dp;
void dfs(int v = 0, int p = -1) {
  vector<LL> delta;
  LL sumnoup = 0;
  for (auto e : graph[v])
    if (e.first != p) {
      int u = e.first;
      dfs(u, v);
      delta.emplace_back(dp[1][u] + e.second - dp[0][u]);
      sumnoup += dp[0][u];
    }
  for (LL &x : delta) x = max(x, 0LL);
  sort(delta.rbegin(), delta.rend());
  0 && cerr;
  0 && cerr;
  for (int it = (0); it <= ((2) - 1); ++it) {
    int sons = (it == 0 ? k : k - 1);
    if (size(delta) > sons) delta.resize(sons);
    LL sumdelta = 0;
    for (LL x : delta) sumdelta += x;
    dp[it][v] = sumnoup + sumdelta;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q-- > 0) {
    cin >> n >> k;
    graph.clear();
    graph.resize(n);
    for (int edge = (0); edge <= ((n - 1) - 1); ++edge) {
      int v, u, w;
      cin >> v >> u >> w;
      --v, --u;
      graph[v].emplace_back(u, w);
      graph[u].emplace_back(v, w);
    }
    dp.clear();
    dp.resize(2, vector<LL>(n));
    dfs(0);
    0 && cerr;
    cout << max(dp[0][0], dp[1][0]) << '\n';
  }
}
