#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx,tune=native")
using namespace std;
const int MOD = 1000000007;
const int UNDEF = -1;
const int INF = 1 << 30;
template <typename T>
inline bool chkmax(T &aa, T bb) {
  return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T &aa, T bb) {
  return aa > bb ? aa = bb, true : false;
}
int rint();
char rch();
long long rlong();
const int mn = (5e5) + 2;
vector<pair<int, int> > g[mn];
long long dp[mn][2];
int klim;
void dfs(int x, int p) {
  long long base = 0;
  vector<long long> add;
  for (auto &it : g[x]) {
    int y = it.first, w = it.second;
    if (y != p) {
      dfs(y, x);
      base += dp[y][0];
      long long alt = dp[y][1] + w - dp[y][0];
      if (alt > 0) {
        add.push_back(alt);
      }
    }
  }
  sort(add.begin(), add.end(), greater<long long>());
  int asz = add.size();
  long long sumadd = 0;
  for (int i = 0; i < min(asz, klim - 1); i++) {
    sumadd += add[i];
  }
  dp[x][1] = base + sumadd;
  if (klim - 1 < asz) sumadd += add[klim - 1];
  dp[x][0] = base + sumadd;
}
void go() {
  int n = rint();
  klim = rint();
  for (int x = 1; x <= n; x++) g[x].clear();
  for (int i = 1; i < n; i++) {
    int x = rint();
    int y = rint();
    int w = rint();
    g[x].push_back(make_pair(y, w));
    g[y].push_back(make_pair(x, w));
  }
  dfs(1, -1);
  long long ans = dp[1][0];
  printf("%lld\n", ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q = rint();
  for (int i = 0; i < q; i++) go();
}
static char stdinBuffer[1024];
static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char *stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
  size_t remaining = stdinDataEnd - stdinPos;
  if (remaining < amount) {
    memmove(stdinBuffer, stdinPos, remaining);
    size_t sz = fread(stdinBuffer + remaining, 1,
                      sizeof(stdinBuffer) - remaining, stdin);
    stdinPos = stdinBuffer;
    stdinDataEnd = stdinBuffer + remaining + sz;
    if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
  }
}
int rint() {
  readAhead(16);
  int x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}
char rch() {
  readAhead(16);
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  char ans = *stdinPos;
  ++stdinPos;
  return ans;
}
long long rlong() {
  readAhead(32);
  long long x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}
