#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n = 0, m = 0, k = 0;
    cin >> n >> m >> k;
    if (m == 0) {
      cout << "0\n";
      continue;
    }
    if (k == n && m > 1) {
      cout << "0\n";
      continue;
    } else if (k == n && m == 1) {
      cout << "1\n";
      continue;
    }
    if ((n / k) >= m) {
      cout << m << "\n";
      continue;
    } else {
      m = m - n / k;
      m = ceil(float(m) / float(k - 1));
      cout << (n / k) - m << "\n";
    }
  }
}
