#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool cmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool cmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
const int _ = 2e5 + 55;
int T, N, K;
int hd[_], nxt[_ << 1], to[_ << 1], tot;
int Q[_], Qhd, Qtl;
int sz[_], d[_];
bool vis[_];
void addedge(int x, int y) { nxt[++tot] = hd[x], hd[x] = tot, to[tot] = y; }
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &N, &K);
    int x, y;
    tot = 0;
    for (int i(1), I(N); i <= I; ++i) hd[i] = sz[i] = d[i] = vis[i] = 0;
    for (int i(1), I(N - 1); i <= I; ++i)
      scanf("%d%d", &x, &y), addedge(x, y), addedge(y, x), ++d[x], ++d[y];
    if (K == 1) {
      printf("%d\n", N - 1);
      continue;
    }
    for (int i(1), I(N); i <= I; ++i)
      if (d[i] == 1)
        for (int j(hd[i]), v(to[j]); j; v = to[j = nxt[j]]) ++sz[v], vis[i] = 1;
    Qhd = 1, Qtl = 0;
    for (int i(1), I(N); i <= I; ++i)
      if (sz[i] && sz[i] % K == 0 && d[i] == sz[i] + 1) Q[++Qtl] = i;
    int ans(0);
    while (Qhd <= Qtl) {
      int u(Q[Qhd++]);
      if (vis[u]) continue;
      vis[u] = 1;
      ans += sz[u] / K;
      for (int i(hd[u]), v(to[i]); i; v = to[i = nxt[i]])
        if (!vis[v]) {
          ++sz[v];
          if (sz[v] % K == 0 && sz[v] == d[v] - 1) Q[++Qtl] = v;
        }
    }
    for (int i(1), I(N); i <= I; ++i)
      if (!vis[i]) ans += sz[i] / K;
    printf("%d\n", ans);
  }
  return 0;
}
