#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    int N, M, K;
    cin >> N >> M >> K;
    if (M == 0) {
      cout << "0\n";
    } else if (M <= (N / K)) {
      cout << M << endl;
    } else {
      if ((M - (N / K)) % (K - 1) == 0) {
        cout << (N / K) - ((M - (N / K)) / (K - 1)) << endl;
      } else {
        cout << (N / K) - ((M - (N / K)) / (K - 1)) - 1 << endl;
      }
    }
  }
  return 0;
}
