#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int i, j, k, l, m, n;
    cin >> n >> m >> k;
    i = n / k;
    j = min(m, i);
    l = (m - j + k - 2) / (k - 1);
    cout << (j - l) << "\n";
  }
  return 0;
}
