#include <bits/stdc++.h>
unsigned long long int GCD(unsigned long long int a, unsigned long long int b) {
  if (b == 0) return a;
  a %= b;
  return GCD(b, a);
}
int main() {
  int t;
  unsigned long long int m, d, w;
  unsigned long long int q, r;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    scanf("%llu%llu%llu", &m, &d, &w);
    w /= GCD(w, d - 1);
    if (d < m) m = d;
    q = m / w;
    r = m % w;
    printf("%llu\n", r * q * (q + 1) / 2 + (w - r) * q * (q - 1) / 2);
  }
  return 0;
}
