#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int d[N], n, k, siz[N], ans;
int head[N], ver[N], net[N], idx;
bool vis[N];
queue<int> q;
void add(int a, int b) { net[++idx] = head[a], ver[idx] = b, head[a] = idx; }
void init() {
  while (!q.empty()) q.pop();
  for (int i = 1; i <= idx; i++) head[i] = net[i] = 0;
  idx = 0;
  for (int i = 1; i <= n; i++) d[i] = siz[i] = vis[i] = 0;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ans = 0;
    scanf("%d%d", &n, &k);
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      add(u, v), add(v, u);
      d[u]++, d[v]++;
    }
    if (k == 1) {
      printf("%d\n", n - 1);
      init();
      continue;
    }
    for (int i = 1; i <= n; i++)
      if (d[i] == 1) siz[ver[head[i]]]++, vis[i] = true;
    for (int i = 1; i <= n; i++)
      if (siz[i] && siz[i] % k == 0 && d[i] - siz[i] == 1) q.push(i);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (vis[u]) continue;
      ans += siz[u] / k, vis[u] = true;
      for (int i = head[u]; i; i = net[i]) {
        int v = ver[i];
        if (!vis[v]) {
          siz[v]++;
          if (siz[v] && siz[v] % k == 0 && d[v] - siz[v] == 1) q.push(v);
        }
      }
    }
    for (int i = 1; i <= n; i++)
      if (!vis[i]) ans += siz[i] / k;
    printf("%d\n", ans);
    init();
  }
}
