#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
long long powm(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
void fukof() {}
void problem() {}
const long long mod = 1e9 + 7;
const long long N = 1000005;
const long long inf = 1e9;
int n, k, ans;
vector<set<int>> g, leaves;
struct comp {
  bool operator()(int a, int b) const {
    if (leaves[a].size() == leaves[b].size()) return a < b;
    return leaves[a].size() > leaves[b].size();
  }
};
void solve() {
  cin >> n >> k;
  g = leaves = vector<set<int>>(n);
  int x, y;
  for (long long i = 0; i < (n - 1); i++) {
    cin >> x >> y;
    x--, y--;
    g[x].insert(y);
    g[y].insert(x);
  }
  for (long long i = 0; i < (n); i++)
    if (g[i].size() == 1) leaves[*g[i].begin()].insert(i);
  set<int, comp> st;
  for (long long i = 0; i < (n); i++) st.insert(i);
  int ans = 0;
  while (true) {
    int v = *st.begin();
    if (leaves[v].size() < k) break;
    for (long long i = 0; i < (k); i++) {
      int lf = *leaves[v].begin();
      g[lf].erase(v);
      g[v].erase(lf);
      st.erase(v);
      st.erase(lf);
      leaves[v].erase(lf);
      if (leaves[lf].count(v)) leaves[lf].erase(v);
      if (g[v].size() == 1) {
        int to = *g[v].begin();
        st.erase(to);
        leaves[to].insert(v);
        st.insert(to);
      }
      st.insert(v);
      st.insert(lf);
    }
    ans++;
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int tc = 1;
  cin >> tc;
  while (tc--) {
    problem();
    fukof();
    solve();
  }
  return 0;
}
