#include <bits/stdc++.h>
using namespace std;
int const MAXN = 2e5 + 10, MAXM = MAXN * 2;
int n, m, T, k;
int vis[MAXN], e[MAXM], ne[MAXM], h[MAXN], idx, d[MAXN], cnt[MAXN];
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> T;
  while (T--) {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) vis[i] = 0, h[i] = -1, d[i] = cnt[i] = 0;
    idx = 0;
    for (int i = 1, a, b; i < n; ++i) {
      cin >> a >> b;
      add(a, b), add(b, a);
      d[a]++, d[b]++;
    }
    queue<int> q;
    for (int i = 1; i <= n; ++i)
      if (d[i] == 1) q.push(i);
    int res = 0;
    while (q.size()) {
      auto t = q.front();
      q.pop();
      vis[t] = 1;
      for (int i = h[t]; ~i; i = ne[i]) {
        int j = e[i];
        if (vis[j]) continue;
        d[j]--, cnt[j]++;
        if (cnt[j] % k == 0) {
          res++;
          if (d[j] == 1) q.push(j);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
