#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long INF = 2 * 1e9 + 10;
const int MOD = 998244353;
int q, n, k;
long long dp[MAXN][2];
vector<vector<pair<int, long long> > > v;
long long calc(int u, int x, int p) {
  vector<pair<long long, int> > profit;
  for (auto to : v[u]) {
    if (to.first != p)
      profit.push_back(
          {dp[to.first][0] + to.second - dp[to.first][1], to.first});
  }
  sort(profit.begin(), profit.end());
  long long sum = 0;
  for (int i = 0; i < profit.size(); i++) {
    sum += dp[profit[i].second][1];
    if (i >= (int)profit.size() - x && profit[i].first > 0)
      sum += profit[i].first;
  }
  return sum;
}
void dfs(int u, int p) {
  if (v[u].size() == 1 && p != -1) {
    return;
  }
  for (auto to : v[u]) {
    if (to.first != p) dfs(to.first, u);
  }
  dp[u][0] = calc(u, k - 1, p);
  dp[u][1] = calc(u, k, p);
}
long long solve() {
  cin >> n >> k;
  v.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int from, to;
    long long w;
    cin >> from >> to >> w;
    v[from - 1].push_back({to - 1, w});
    v[to - 1].push_back({from - 1, w});
  }
  dfs(0, -1);
  return dp[0][1];
}
void clearr() {
  v.clear();
  for (int i = 0; i < n; i++) {
    dp[i][0] = 0;
    dp[i][1] = 0;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cout << solve() << '\n';
    clearr();
  }
  return 0;
}
