#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double eps = (1e-5);
int isGreater(long double x, long double y) {
  if (abs(x - y) < eps) return 0;
  if (x > y) return 1;
  return -1;
}
const int MAX_N = 5e5 + 5;
vector<pair<int, int> > adj[MAX_N];
vector<vector<long long> > mem[MAX_N];
int n, k;
void clear() {
  for (int i = 0; i < n; ++i) {
    adj[i].clear();
    mem[i].clear();
  }
}
long long f(int u, int i, int minus, int p) {
  if (i == (int)((adj[u]).size())) return 0;
  long long &ret = mem[u][i][minus];
  if (~ret) return ret;
  ret = 0;
  vector<long long> s;
  for (auto e : adj[u]) {
    int v = e.first;
    if (v == p) continue;
    ret += f(v, 0, 0, u);
    s.push_back(f(v, 0, 1, u) + e.second - f(v, 0, 0, u));
  }
  sort(s.rbegin(), s.rend());
  for (int j = 0; j < min(k - minus, (int)((s).size())); ++j) {
    ret += max(0LL, s[j]);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> k;
    clear();
    for (int i = 0; i < n - 1; ++i) {
      int u, v, w;
      cin >> u >> v >> w;
      --u;
      --v;
      adj[u].push_back({v, w});
      adj[v].push_back({u, w});
      mem[u].push_back(vector<long long>(2, -1));
      mem[v].push_back(vector<long long>(2, -1));
    }
    cout << f(0, 0, 0, -1) << '\n';
  }
  return 0;
}
