#include <bits/stdc++.h>
using namespace std;
bool checkPalin(string s) {
  string res = "";
  if (s.size() == 1) {
    return true;
  }
  for (long long i = s.size() - 1; i >= 0; i--) {
    res += s[i];
  }
  if (res == s) {
    return true;
  } else {
    return false;
  }
}
void addEdge(vector<int> adj[], int u, int v) {
  adj[u].push_back(v);
  adj[v].push_back(u);
}
long long binarySearch(vector<long long> v, long long l, long long r,
                       long long x) {
  if (r >= 1) {
    long long mid = l + (r - l) / 2;
    if (v[mid] == x)
      return mid;
    else if (v[mid] > x)
      return binarySearch(v, l, mid - 1, x);
    return binarySearch(v, mid + 1, r, x);
  }
  return -1;
}
long long linear(long long arr[], long long n, long long val) {
  long long count = 0;
  for (long long i = 0; i < n; i++) {
    count++;
    if (arr[i] == val) return count;
  }
}
long long ma(string n) {
  if (n.length() == 1) return stoll(n, nullptr, 10);
  long long m = n[0] - '0';
  for (long long i = 1; i < n.length(); i++) {
    m = max(m, (long long)n[i] - '0');
  }
  return m;
}
long long mi(string n) {
  if (n.length() == 1) return stoll(n, nullptr, 10);
  long long m = n[0] - '0';
  for (long long i = 1; i < n.length(); i++) {
    m = min(m, (long long)n[i] - '0');
  }
  return m;
}
long long t;
long long n, m, k;
int main() {
  cin >> t;
  while (t-- && cin >> n >> m >> k) {
    long long a = n / k;
    if (a >= m) {
      cout << m << "\n";
      continue;
    }
    cout << a - ceil((double)(m - a) / (k - 1)) << "\n";
  }
  return 0;
}
