#include <bits/stdc++.h>
const double PI = acos(-1);
template <class A, class B>
std::ostream& operator<<(std::ostream& st, const std::pair<A, B> p) {
  st << "(" << p.first << ", " << p.second << ")";
  return st;
}
using namespace std;
const int MAX = 1 << 20;
int n;
int fr[MAX], to[MAX], W[MAX];
int cnt[MAX];
std::vector<int> E[MAX];
long long dp[MAX][2];
int K, visNum;
int visID[MAX][2];
vector<pair<long long, long long>> tmp[MAX][2];
long long solve(int u, int p, int c) {
  long long& ret = dp[u][c];
  if (visID[u][c] == visNum) return ret;
  visID[u][c] = visNum;
  ret = 0;
  auto& V = tmp[u][c];
  V.clear();
  for (auto e : E[u]) {
    int v = fr[e] + to[e] - u;
    if (v == p) continue;
    long long g1 = solve(v, u, 1) + W[e], g2 = solve(v, u, 0);
    V.emplace_back(g1 - g2, g2);
  }
  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());
  int target = K - c;
  while (((int)V.size()) > target) {
    auto p = V.back();
    V.pop_back();
    ret += p.second;
  }
  for (auto p : V) ret += max(p.first + p.second, p.second);
  return ret;
}
int main() {
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d %d", &n, &K);
    for (int u = 1; u <= n; u++) E[u].clear();
    for (int e = 0; e < (n - 1); e++) {
      scanf("%d %d %d", fr + e, to + e, W + e);
      E[fr[e]].push_back(e);
      E[to[e]].push_back(e);
    }
    visNum++;
    printf("%lld\n", solve(1, 0, 0));
  }
  return 0;
}
