#include <bits/stdc++.h>
template <class C>
inline void log_c(const C& c) {}
template <class C>
inline int sz(const C& c) {
  return static_cast<int>(c.size());
}
using namespace std;
using pii = pair<int, int>;
const std::string eol = "\n";
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int test_sample_count;
  cin >> test_sample_count;
  while (test_sample_count--) {
    int n, m, k;
    cin >> n >> m >> k;
    int max_c = min(n / k, m);
    int rest = m - max_c;
    int max_rest = (rest + k - 2) / (k - 1);
    cout << (max_c - max_rest) << eol;
  }
  return 0;
}
