#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt, ii = 0;
  cin >> tt;
  while (ii++ < tt) {
    long long n, m, k;
    cin >> n >> m >> k;
    long long c = n / k;
    if (c >= m)
      cout << m << '\n';
    else {
      if ((m - c) % (k - 1) == 0)
        cout << c - (m - c) / (k - 1) << '\n';
      else
        cout << c - (m - c) / (k - 1) - 1 << '\n';
    }
  }
  cerr << "[Execution : " << (1.0 * clock()) / CLOCKS_PER_SEC << "s]\n";
  return 0;
}
