#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
void test() {
  cin >> n >> m >> k;
  vector<long long> ma(k);
  ma[0] = min(n / k, m);
  m -= n / k;
  for (long long i = 0; i < m; ++i) ma[i % (k - 1) + 1]++;
  cout << ma[0] - ma[1] << "\n";
}
int32_t main() {
  long long t;
  cin >> t;
  while (t--) test();
  return 0;
}
