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