#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
using v2 = vector<vector<T>>;
template <typename T>
inline v2<T> init(int r, int c, const T& t) {
  return v2<T>(r, vector<T>(c, t));
}
int n, k;
void solve() {
  cin >> n >> k;
  v2<int> adj(n), leaves(n);
  vector<int> deg(n, 0);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    ++deg[a], ++deg[b];
  }
  if (k == 1) {
    cout << n - 1 << '\n';
    return;
  }
  vector<bool> act(n, false), done(n, false);
  queue<int> q;
  for (int i = 0; i < n; ++i) {
    for (int next : adj[i]) {
      if ((int)adj[next].size() == 1) leaves[i].push_back(next);
    }
    if ((int)leaves[i].size() >= k) {
      q.push(i);
      act[i] = true;
    }
  }
  int ans = 0;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    if (done[node]) continue;
    act[node] = false;
    int s = leaves[node].size();
    for (int i = 0; i < k; ++i) {
      assert(!done[leaves[node].back()]);
      done[leaves[node].back()] = true;
      leaves[node].pop_back();
    }
    s -= k, deg[node] -= k;
    if (s >= k) {
      act[node] = true;
      q.push(node);
    } else if (deg[node] == 1) {
      for (int next : adj[node]) {
        if (!done[next]) {
          leaves[next].push_back(node);
          if ((int)leaves[next].size() >= k) {
            if (!act[next]) {
              act[next] = true;
              q.push(next);
            }
          }
        }
      }
    }
    ++ans;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i) {
    solve();
  }
  return 0;
}
