#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  T s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) print(x / 10);
  putchar(x % 10 + '0');
}
const int N = 2e5 + 100;
vector<int> v[N];
int in[N], siz[N];
int n, k, ans;
void del(int x, int fa) {
  if (siz[x] < k) return;
  int t = siz[x] / k;
  siz[x] %= k;
  ans += t;
  in[x] -= t * k;
  if (in[x] == 1) {
    for (int j = 0, len = v[x].size(); j < len; ++j) {
      if (v[x][j] == fa || in[v[x][j]] == 1) continue;
      ++siz[v[x][j]];
      del(v[x][j], x);
    }
  }
}
void solve() {
  n = read<int>(), k = read<int>();
  ans = 0;
  for (register int i = int(1); i <= (n); ++i) {
    in[i] = 0, siz[i] = 0;
    v[i].clear();
  }
  for (register int i = int(2); i <= (n); ++i) {
    int x = read<int>(), y = read<int>();
    v[x].push_back(y);
    v[y].push_back(x);
    ++in[x];
    ++in[y];
  }
  if (n == 2) {
    if (k >= 2) {
      print(-1), putchar(10);
    } else
      print(1), putchar(10);
    return;
  }
  for (register int i = int(1); i <= (n); ++i) {
    if (in[i] == 1) {
      for (int j = 0, len = v[i].size(); j < len; ++j) {
        ++siz[v[i][j]];
      }
    }
  }
  for (register int i = int(1); i <= (n); ++i) del(i, 0);
  print(ans), putchar(10);
}
int main() {
  int t;
  t = read<int>();
  while (t--) solve();
}
