#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int> a, const pair<int, int> b) {
  return a.first < b.first && a.second != b.second;
}
void printV(vector<int> nums) {
  for (int i : nums) {
    printf("%d ", i);
  }
  printf("\n");
}
bool isPrime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
long long int nCr(int n, int r) {
  if (n == r) {
    return 1;
  }
  long long int ans = 1;
  if (n > r) {
    int mn = min(n - r, r);
    int mx = max(n - r, r);
    for (int i = n; i > mx; i--) {
      ans = ans * i;
    }
    for (int i = 1; i <= mn; i++) {
      ans /= i;
    }
    return ans;
  }
  return 0;
}
vector<int> sieve(int n) {
  vector<bool> is_prime(n + 1, true);
  vector<int> prime;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i] && (long long int)i * i <= n) {
      for (int j = i * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    if (is_prime[i]) prime.push_back(i);
  }
  return prime;
}
vector<int> nextSmallerElements(vector<int> nums) {
  int n = nums.size();
  stack<int> stk;
  stk.push(0);
  vector<int> ind(n);
  for (int i = 1; i < n; i++) {
    if (stk.empty()) {
      stk.push(i);
    } else {
      while (!stk.empty() && nums[stk.top()] > nums[i]) {
        ind[stk.top()] = i;
        stk.pop();
      }
      stk.push(i);
    }
  }
  while (!stk.empty()) {
    ind[stk.top()] = -1;
    stk.pop();
  }
  return ind;
}
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool valid(int i, int j, int n, int m) {
  if (i >= 0 && i < n && j >= 0 && j < m)
    return true;
  else
    return false;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int win = min(n / k, m);
    m -= win;
    m = (int)ceil(m / (float)(k - 1));
    if (win == m)
      cout << 0 << endl;
    else
      cout << win - m << endl;
  }
  return 0;
}
