#include <bits/stdc++.h>
const int maxn = 2e5 + 5;
struct node {
  int v, nt, u, pr;
} e[maxn << 1];
int head[maxn], del[maxn], deg[maxn];
int st[maxn], st1[maxn], tp1, tp, n, k, vis[maxn];
void addedge(int u, int v, int pos) {
  e[pos].u = u, e[pos].v = v;
  e[pos + n].v = u, e[pos + n].u = v;
  e[pos].nt = head[u], e[pos + n].nt = head[v];
  e[pos].pr = e[pos + n].pr = 0;
  e[head[u]].pr = pos, e[head[v]].pr = pos + n;
  head[u] = pos, head[v] = pos + n;
  deg[u]++, deg[v]++;
}
void de1(int pos) {
  int pr = e[pos].pr, nt = e[pos].nt;
  if (pr) e[pr].nt = nt;
  if (nt) e[nt].pr = pr;
  if (pos == head[e[pos].u]) head[e[pos].u] = nt;
}
void de(int u) {
  int pos = head[u];
  if (!pos) return;
  if (pos > n)
    de1(pos - n);
  else
    de1(pos + n);
  deg[u]--;
  del[e[pos].v]++;
  if (del[e[pos].v] >= k && !vis[e[pos].v]) {
    st1[++tp1] = e[pos].v;
    vis[e[pos].v] = 1;
  }
}
void print1() {
  for (int i = 1; i <= n; i++) printf("%d ", deg[i]);
  puts("");
  for (int i = 1; i <= n; i++) printf("%d ", del[i]);
  puts("");
}
void print() {
  for (int i = 1; i <= n; i++) {
    printf("%d :", i);
    for (int pos = head[i]; pos; pos = e[pos].nt) printf("%d ", e[pos].v);
    puts("");
  }
  printf("st1: ");
  for (int i = 1; i <= tp1; i++) printf("%d ", st1[i]);
  puts("");
  printf("st: ");
  for (int i = 1; i <= tp; i++) printf("%d ", st[i]);
  puts("");
  print1();
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) head[i] = deg[i] = del[i] = 0;
    int ans = 0;
    tp = tp1 = 0;
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      addedge(u, v, i);
    }
    for (int i = 1; i <= n; i++)
      if (deg[i] == 1) st[++tp] = i;
    do {
      while (tp) de(st[tp--]);
      while (tp1) {
        int u = st1[tp1--];
        if (del[u] < k) continue;
        ans += del[u] / k;
        deg[u] -= del[u] / k * k;
        if (deg[u] == 1) st[++tp] = u;
        del[u] %= k;
        vis[u] = 0;
      }
    } while (tp);
    printf("%d\n", ans);
  }
  return 0;
}
