#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 6;
const long long INF = 1e15, mod = 998244353;
const int inf = 1e9;
vector<vector<pair<long long, long long>>> g;
vector<vector<long long>> dp;
struct rt {
  long long w, u, notu, vertex;
};
int n, k;
void dfs(int v, int p = -1) {
  for (pair<long long, long long> u : g[v])
    if (u.first != p) dfs(u.first, v);
  vector<rt> ch;
  for (pair<long long, long long> u : g[v])
    if (u.first != p)
      ch.push_back({u.second, dp[u.first][1], dp[u.first][0], u.first});
  sort(ch.begin(), ch.end(),
       [&](rt a, rt b) { return a.w + a.u - a.notu > b.w + b.u - b.notu; });
  long long ans1 = 0, ans0 = 0;
  for (int i = (0); i < (int(ch.size())); ++i) {
    rt a = ch[i];
    if (i < k && a.w + a.u - a.notu > 0)
      ans0 += a.w + a.u;
    else
      ans0 += a.notu;
    if (i < k - 1 && a.w + a.u - a.notu > 0)
      ans1 += a.w + a.u;
    else
      ans1 += a.notu;
  }
  dp[v][0] = ans0, dp[v][1] = ans1;
}
void solve() {
  g.clear();
  dp.clear();
  cin >> n >> k;
  g.resize(n);
  dp.resize(n, vector<long long>(2));
  for (int i = (0); i < (n - 1); ++i) {
    long long v, u, w;
    cin >> v >> u >> w;
    v--;
    u--;
    g[v].push_back({u, w});
    g[u].push_back({v, w});
  }
  dfs(0);
  cout << dp[0][0] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(20);
  int T;
  cin >> T;
  while (T--) solve();
}
