#include <bits/stdc++.h>
using namespace std;
int t, n, m, k, mk[50];
int main() {
  cin >> t;
  while (t--) {
    mk[0] = mk[1] = 0;
    cin >> n >> m >> k;
    int card = 0, mxm = 0, i = 0;
    card = n / k;
    while (m > 0) {
      while (m > 0 && mxm != card) {
        m--;
        mxm++;
        mk[i]++;
      }
      i++;
      mk[1] = ceil(float(m) / (k - 1));
      m = 0;
    }
    cout << mk[0] - mk[1] << '\n';
  }
  return 0;
}
