#include <bits/stdc++.h>
using namespace std;
long long f[1000010];
int main() {
  long long n, m, k, t;
  for (int i = 2; i <= 1000000; i++)
    if (f[i] == 0)
      for (int j = i; j <= 1000000; j += i)
        if (f[j] == 0) f[j] = i;
  cin >> t;
  while (t--) {
    cin >> n >> m >> k;
    int x = n / k;
    n -= x;
    if (x >= m)
      x = m;
    else {
      m -= x;
      if (m % (k - 1) == 0)
        x -= m / (k - 1);
      else
        x -= (m / (k - 1) + 1);
    }
    cout << x << endl;
  }
}
