#include <bits/stdc++.h>
using namespace std;
void solve(long long *arr, long long n) {
  sort(arr, arr + n);
  long long ans = 1, val = 1;
  map<long long, long long> mp;
  for (long long i = 0; i < n; i++) mp[arr[i]] += 1;
  if (arr[0] > 1) {
    bool flag = false;
    for (auto e : mp)
      if (e.second > 1) {
        flag = true;
        break;
      }
    if (!flag) {
      cout << 1 << "\n";
      return;
    }
  }
  vector<long long> visited(n, false);
  for (long long i = 0; i < n;) {
    if (visited[i]) continue;
    long long j = i, k = i;
    while (j < n) {
      if (!visited[j] && ans + j - i >= arr[j]) {
        val = max(val, ans + j - i + 1);
        visited[j] = true;
        k = j;
      }
      j += 1;
    }
    i = k + 1;
  }
  cout << val << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m, k, each;
    cin >> n >> m >> k;
    each = n / k;
    if (each >= m)
      cout << m << "\n";
    else {
      long long ans = each;
      m -= each;
      if (m % (k - 1) == 0)
        ans -= (m / (k - 1));
      else
        ans -= (m / (k - 1)) + 1;
      cout << ans << "\n";
    }
  }
  return 0;
}
