#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010, maxm = 400010;
int T, n, in[maxn], k, h[maxn], to[maxm], nt[maxm], cnt, book[maxn], l[maxn];
void add(int x, int y) {
  to[cnt] = y;
  nt[cnt] = h[x];
  h[x] = cnt++;
}
int f() {
  queue<int> q;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 1) {
      book[i] = 1;
      q.push(i);
    }
  }
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int i = h[p]; ~i; i = nt[i]) {
      int v = to[i];
      l[v]++;
      if (l[v] >= k) {
        l[v] -= k;
        in[v] -= k;
        ans++;
      }
      if (!book[v] && in[v] == 1) {
        book[v] = 1;
        q.push(v);
      }
    }
  }
  return ans;
}
int main(void) {
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
      in[i] = book[i] = l[i] = 0;
      h[i] = -1;
    }
    cnt = 0;
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      add(x, y);
      add(y, x);
      in[x]++;
      in[y]++;
    }
    if (k == 1)
      printf("%d\n", n - 1);
    else
      printf("%d\n", f());
  }
  return 0;
}
