#include <bits/stdc++.h>
using namespace std;
long long ara[200005];
long long aray[200005];
int main() {
  long long a, b, c, d, t, i, j, k, n, m;
  cin >> t;
  while (t--) {
    long long ans = 0;
    cin >> n >> m >> k;
    a = n / k;
    if (m > a) {
      m -= a;
      if (m % (k - 1) == 0) {
        ans = a - ((m / (k - 1)));
      } else {
        ans = a - ((m / (k - 1))) - 1;
      }
    } else {
      ans = m;
    }
    cout << ans << endl;
  }
  return 0;
}
