#include <bits/stdc++.h>
using namespace std;
int t;
int n, m, k;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> m >> k;
    int max_point = min(n / k, m);
    int min_point =
        (m - max_point) / (k - 1) + ((m - max_point) % (k - 1) != 0);
    cout << max_point - min_point << '\n';
  }
}
