#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 10;
struct edge {
  long long nt, to;
} E[MAXN];
long long N, K, T, head[MAXN], cnt = -1, ind[MAXN], siz[MAXN], vis[MAXN], ans;
queue<long long> q;
void add(long long, long long);
int main() {
  scanf("%lld", &T);
  while (T--) {
    ans = 0, cnt = -1;
    scanf("%lld%lld", &N, &K);
    for (long long i = 0; i <= N + 5; i++)
      ind[i] = 0, siz[i] = 0, vis[i] = 0, head[i] = -1;
    for (long long i = 1, x, y; i < N; i++) {
      scanf("%lld%lld", &x, &y);
      add(x, y);
      add(y, x);
      ind[x]++, ind[y]++;
    }
    if (K == 1) {
      printf("%lld\n", N - 1);
      continue;
    }
    for (long long i = 1; i <= N; i++)
      if (ind[i] == 1) {
        for (long long j = head[i]; ~j; j = E[j].nt) {
          long long v = E[j].to;
          siz[v]++;
        }
        vis[i] = 1;
      }
    for (long long i = 1; i <= N; i++)
      if (siz[i] && siz[i] % K == 0 && ind[i] - siz[i] == 1) q.push(i);
    while (!q.empty()) {
      long long nt = q.front();
      q.pop();
      if (vis[nt]) continue;
      ans += (siz[nt] / K);
      vis[nt] = 1;
      for (long long i = head[nt]; ~i; i = E[i].nt) {
        long long v = E[i].to;
        if (!vis[v]) {
          siz[v]++;
          if (siz[v] % K == 0 && ind[v] - siz[v] == 1) q.push(v);
        }
      }
    }
    for (long long i = 1; i <= N; i++)
      if (!vis[i]) ans += (siz[i] / K);
    printf("%lld\n", ans);
  }
  return 0;
}
void add(long long x, long long y) {
  cnt++;
  E[cnt].to = y;
  E[cnt].nt = head[x];
  head[x] = cnt;
}
