#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, res = 0;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  while (c >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c ^ 48), c = getchar();
  return f * res;
}
int main() {
  int T = read();
  for (; T; T--) {
    int n = read(), m = read(), k = read();
    int num = n / k;
    if (m <= num)
      cout << m << '\n';
    else
      cout << num - (m - num + k - 2) / (k - 1) << '\n';
  }
  return 0;
}
