#include <bits/stdc++.h>
using namespace std;
long long T, m, d, w, mn, ans, gg;
inline long long gcd(long long u, long long v) {
  for (; u && v && u != v;) {
    u %= v;
    swap(u, v);
  }
  return max(u, v);
}
inline long long C2(long long u) { return u * (u - 1) / 2; }
int main() {
  long long i, j, t, p;
  cin >> T;
  while (T--) {
    scanf("%lld%lld%lld", &m, &d, &w);
    mn = min(m, d);
    if (mn == 1) {
      puts("0");
      continue;
    }
    gg = w / gcd(w, d - 1);
    t = mn % gg;
    p = mn / gg;
    printf("%lld\n", t * C2(p + 1) + (gg - t) * C2(p));
  }
}
