#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
using ll = long long;
const ll mod = 1000000007;
vector<pi> v[500005];
vector<ll> t[500005];
int n, k;
ll dp[2][500005];
void f(int cur, int par) {
  dp[0][cur] = dp[1][cur] = 0;
  t[cur].clear();
  ll ret = 0;
  for (pi p : v[cur]) {
    int nxt = p.first;
    int cc = p.second;
    if (nxt == par) continue;
    f(nxt, cur);
    ll op_no = dp[0][nxt];
    ll op_yes = dp[1][nxt] + cc;
    ret += op_no;
    if (op_yes - op_no > 0) t[cur].push_back(op_yes - op_no);
  }
  sort(t[cur].begin(), t[cur].end(), greater<ll>());
  for (int i = 0; i < t[cur].size(); i++) {
    if (t[cur][i] < 0) break;
    if (i < k - 1) dp[1][cur] += t[cur][i];
    if (i < k)
      dp[0][cur] += t[cur][i];
    else
      break;
  }
  dp[1][cur] += ret;
  dp[0][cur] += ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) v[i].clear();
    for (int i = 1; i < n; i++) {
      int a, b, d;
      cin >> a >> b >> d;
      v[a].push_back({b, d});
      v[b].push_back({a, d});
    }
    f(1, 0);
    cout << dp[0][1] << '\n';
  }
}
