#include <bits/stdc++.h>
using namespace std;
const long long mx = 1e6 + 9;
const long long inf = 1e18 + 9;
long long A[mx], cnt[mx];
bool sign[mx];
map<long long, long long> mp;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long tc;
  cin >> tc;
  while (tc--) {
    long long n = 0, m = 0, k, x, y, sum = 0, mini = inf, maxi = -inf;
    bool f1 = false, f2 = false;
    string s1, s2;
    cin >> n >> m >> k;
    x = n / k;
    if (x >= m)
      cout << m << endl;
    else {
      y = m - x;
      sum = y / (k - 1);
      if (y % (k - 1) != 0) {
        sum++;
      }
      cout << x - sum << endl;
    }
  }
}
