#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
vector<pair<long long, long long> > g[N];
int n, k;
long long dp[N][2];
bool used[N];
void dfs(int v, int p) {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      qu;
  for (auto &it : g[v]) {
    if (it.first == p) continue;
    dfs(it.first, v);
    qu.push({dp[it.first][1] + it.second - dp[it.first][0], it.first});
    if (qu.size() > k) qu.pop();
  }
  if (qu.size() == k && qu.top().first > 0) dp[v][1] = -qu.top().first;
  while (!qu.empty()) {
    if (qu.top().first <= 0) {
      qu.pop();
      continue;
    }
    dp[v][0] += qu.top().first;
    dp[v][1] += qu.top().first;
    qu.pop();
  }
  for (auto &it : g[v]) {
    if (it.first == p) continue;
    dp[v][0] += dp[it.first][0];
    dp[v][1] += dp[it.first][0];
  }
}
void solve() {
  for (int i = 0; i <= n; ++i)
    g[i].clear(), dp[i][0] = dp[i][1] = 0, used[i] = false;
  cin >> n >> k;
  for (int i = 1; i < n; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs(0, 0);
  cout << max(dp[0][0], dp[0][1]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    solve();
    cout << "\n";
  }
  return 0;
}
