#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const int dir[2][4] = {{1, -1, 0, 0}, {0, 0, -1, 1}};
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int mx = min(m, n / k);
    m -= min(m, n / k);
    int c = m / (k - 1);
    if (m % (k - 1)) c++;
    cout << mx - c << endl;
  }
  return 0;
}
