#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int man = 2e5 + 10;
const long long mod = 1e9 + 7;
int du[man], vis[man], sz[man];
vector<int> sp[man];
signed main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      du[i] = sz[i] = vis[i] = 0;
      sp[i].clear();
    }
    for (int i = 1; i < n; ++i) {
      int u, v;
      scanf("%d%d", &u, &v);
      du[u]++;
      du[v]++;
      sp[u].push_back(v);
      sp[v].push_back(u);
    }
    queue<int> q;
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
      if (du[i] == 1) {
        q.push(i);
      }
    while (q.size()) {
      int u = q.front();
      q.pop();
      vis[u] = 1;
      for (auto v : sp[u]) {
        if (vis[v]) continue;
        du[v]--;
        sz[v]++;
        if (sz[v] % k == 0) {
          ans++;
        }
        if (du[v] == 1 && sz[v] % k == 0) {
          q.push(v);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
