#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
const int maxn = 510000;
long long dp[maxn][2];
vector<pair<int, int> > e[maxn];
int n, k;
void dfs(int v, int p) {
  long long base = 0;
  vector<long long> difs;
  for (auto w : e[v]) {
    int u = w.first, c = w.second;
    if (u == p) continue;
    dfs(u, v);
    base += dp[u][1];
    difs.push_back(dp[u][0] + c - dp[u][1]);
  }
  sort((difs).rbegin(), (difs).rend());
  dp[v][0] = dp[v][1] = base;
  for (int i = 0; i < (int)(difs.size()); ++i) {
    if (i >= k) break;
    base += difs[i];
    if (i < k - 1) uax(dp[v][0], base);
    if (i < k) uax(dp[v][1], base);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  int q;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    for (int i = 0; i < (int)(n); ++i) e[i].clear();
    for (int i = 0; i < (int)(n - 1); ++i) {
      int u, v, w;
      cin >> u >> v >> w;
      --u;
      --v;
      e[v].push_back({u, w});
      e[u].push_back({v, w});
    }
    for (int i = 0; i < (int)(n); ++i)
      for (int j = 0; j < (int)(2); ++j) dp[i][j] = 0;
    dfs(0, -1);
    cout << dp[0][1] << '\n';
  }
  return 0;
}
