#include <bits/stdc++.h>
using namespace std;
const long long dx[] = {0, 1, 0, -1};
const long long dy[] = {1, 0, -1, 0};
const long long inf = 2e9;
const long long mod = 1e9 + 7;
const long long N = 1e6 + 11;
const long long INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);
long long n, k, M[N], sz[N], viz[N];
vector<long long> v[N];
void solve() {
  cin >> n >> k;
  long long ans = 0;
  for (long long i = 1, x, y; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
    M[x]++;
    M[y]++;
  }
  queue<long long> q;
  for (long long i = 1; i <= n; i++) {
    if (M[i] == 1) q.push(i);
  }
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    viz[x] = 1;
    for (auto it : v[x]) {
      if (viz[it]) continue;
      M[it]--;
      sz[it]++;
      if (sz[it] % k == 0) {
        ans++;
        if (M[it] == 1) {
          q.push(it);
        }
      }
    }
  }
  cout << ans << '\n';
  for (long long i = 1; i <= n; i++) {
    v[i].clear();
    viz[i] = 0;
    M[i] = 0;
    sz[i] = 0;
  }
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(6) << fixed;
  long long T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
}
