#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int m, n, c = 0, k = 0, a = 0, b = 0, temp;
    long long int i, j;
    cin >> n >> m >> k;
    a = min(m, n / k);
    b = (m - a + k - 2) / (k - 1);
    cout << abs(a - b) << endl;
  }
}
