#include <bits/stdc++.h>
using namespace std;
set<int> mapa[200010];
set<int> rleaves[200010];
bool isleaves[200010];
bool inque[200010];
queue<int> can;
int t, n, k, a, b, now, tmp, ans;
void DFS(int I, int source) {
  if (mapa[I].size() == 1) {
    isleaves[I] = 1;
  } else {
    for (set<int>::iterator i = mapa[I].begin(); i != mapa[I].end(); i++) {
      if (*i != source) {
        DFS(*i, I);
        if (isleaves[*i] == 1) {
          rleaves[I].insert(*i);
        }
      }
    }
    if (rleaves[I].size() >= k) {
      can.push(I);
      inque[I] = 1;
    }
  }
}
int main() {
  cin >> t;
  while (t--) {
    ans = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      mapa[i].clear();
      rleaves[i].clear();
      isleaves[i] = 0;
      inque[i] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> a >> b;
      mapa[a].insert(b);
      mapa[b].insert(a);
    }
    if (k == 1) {
      cout << n - 1 << '\n';
    } else {
      now = 1;
      while (mapa[now].size() == 1) {
        now++;
      }
      DFS(now, 0);
      while (!can.empty()) {
        now = can.front();
        can.pop();
        inque[now] = 0;
        while (rleaves[now].size() >= k) {
          for (int i = 0; i < k; i++) {
            tmp = *rleaves[now].begin();
            rleaves[now].erase(tmp);
            mapa[now].erase(tmp);
          }
          ans++;
        }
        if (mapa[now].size() == 1) {
          isleaves[now] = 1;
          rleaves[*mapa[now].begin()].insert(now);
          if (inque[*mapa[now].begin()] == 0 &&
              rleaves[*mapa[now].begin()].size() >= k) {
            can.push(*mapa[now].begin());
          }
        }
      }
      cout << ans << '\n';
    }
  }
  return 0;
}
