#include <bits/stdc++.h>
using namespace std;
namespace FGF {
int n, m;
const int N = 2e5 + 5;
vector<int> g[N];
queue<int> q;
int a[N], cnt[N];
bool vis[N];
void work() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    int ans = 0;
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      g[u].push_back(v), g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++)
      if (g[i].size() == 1) q.push(i);
    while (q.size()) {
      int u = q.front();
      q.pop();
      if (vis[u]) continue;
      vis[u] = 1;
      for (int sz = g[u].size(), i = 0; i < sz; i++) {
        int v = g[u][i];
        if (vis[v]) continue;
        cnt[v]++;
        if (cnt[v] + 1 == g[v].size() && cnt[v] % m == 0) q.push(v);
      }
    }
    for (int i = 1; i <= n; i++) ans += cnt[i] / m;
    for (int i = 1; i <= n; i++) g[i].clear(), cnt[i] = 0, vis[i] = 0;
    printf("%d\n", min(n - 1, ans));
  }
}
}  // namespace FGF
int main() {
  FGF::work();
  return 0;
}
