#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <typename T>
void _dbg(const char* _s, T _h) {
  cerr << _s << " = " << _h << "\n";
}
template <typename T, typename... Ts>
void _dbg(const char* _s, T _h, Ts... _t) {
  for (int _b = 0; ((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',';)
    cerr << *_s++;
  cerr << " = " << _h << ",";
  _dbg(_s + 1, _t...);
}
const long long N = 500 * 1000 + 17;
const long long MOD = 1000 * 1000 * 1000 + 7;
long long n, k;
vector<pair<long long, long long> > g[N];
long long dp[N][2];
bool read() {
  if (!(cin >> n >> k)) return false;
  for (long long i = 0; i < (long long)(n); ++i) g[i].clear();
  for (long long i = 0; i < (long long)(n - 1); ++i) {
    long long u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  return true;
}
void dfs(long long v, long long p, long long wp) {
  vector<long long> df;
  long long sum = 0;
  for (auto& [to, wto] : g[v]) {
    if (to == p) continue;
    dfs(to, v, wto);
    sum += dp[to][0];
    df.emplace_back(dp[to][1] - dp[to][0]);
  }
  sort(df.begin(), df.end());
  reverse(df.begin(), df.end());
  dp[v][0] = sum;
  dp[v][1] = sum + wp;
  for (long long i = 0; i < df.size(); ++i) {
    sum += df[i];
    if (i < k) dp[v][0] = max(dp[v][0], sum);
    if (i < k - 1) dp[v][1] = max(dp[v][1], sum + wp);
  }
}
void solve() {
  dfs(0, 0, 0);
  cout << max(dp[0][0], dp[0][1]) << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long ttt;
  cin >> ttt;
  while (read()) solve();
  return 0;
}
