#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int temp = n / k;
  if (m <= temp) {
    cout << m << "\n";
  } else {
    int mm = m - temp;
    int kk = k - 1;
    if (mm % kk == 0) {
      cout << temp - (mm / kk) << "\n";
    } else {
      cout << temp - ((mm / kk) + 1) << "\n";
    }
  }
}
int main() {
  int t;
  while (cin >> t) {
    while (t--) {
      solve();
    }
  }
}
