#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int v = n / k;
    if (m <= n / k)
      cout << m << endl;
    else if (m == 0 || m == n / k || m == n)
      cout << 0 << endl;
    else {
      int j = v;
      int h = m - v;
      int q = h / (k - 1) + 1;
      if (h % (k - 1) == 0) q--;
      cout << abs(j - q) << endl;
    }
  }
  return 0;
}
