#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 rest = m - temp;
    int y = rest / (k - 1);
    if (rest % (k - 1)) y++;
    cout << temp - y << "\n";
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
