#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 1;
const long long MOD = 1e9 + 7;
const long long MAXN = 5e5 + 5;
const long double EPS = 1e-12;
const long double PI = acos(-1);
const long long XX[] = {0, -1, 0, 1, 0, -1};
const long long YY[] = {-1, 0, 1, 0, -1, 0};
const long long ALPH = 31;
void bye() {
  cout << "\n";
  exit(0);
}
long long n, k;
long long dp[2][MAXN];
vector<pair<long long, long long> > g[MAXN];
void dfs(long long v, long long p = -1) {
  long long sum = 0;
  vector<long long> push;
  for (auto kek : g[v]) {
    long long to = kek.first;
    long long w = kek.second;
    if (to == p) continue;
    dfs(to, v);
    sum += dp[0][to];
    push.push_back(dp[1][to] + w - dp[0][to]);
  }
  sort((push).begin(), push.end());
  reverse((push).begin(), push.end());
  for (int i = 0; i < min((long long)(push).size(), k); i++) {
    if (push[i] > 0) {
      sum += push[i];
    }
  }
  dp[0][v] = dp[1][v] = sum;
  if (k - 1 < (long long)(push).size() && push[k - 1] > 0) {
    dp[1][v] -= push[k - 1];
  }
}
void solve() {
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    g[i].clear();
  }
  for (long long i = 1; i < n; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs(0);
  cout << dp[0][0] << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long TEST_COUNT = 1;
  cin >> TEST_COUNT;
  while (TEST_COUNT--) {
    solve();
  }
  bye();
}
