#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}
int main() {
  long long T, N, M, K, t, ans = 0, i, xx;
  scanf("%lld", &T);
  while (T--) {
    ans = 0;
    scanf("%lld %lld %lld", &N, &M, &K);
    t = K / GCD(K, M - 1);
    xx = min(M, N) / t;
    ans = ((min(M, N) - t) + (min(M, N) - t * xx)) * (xx) / 2;
    printf("%lld\n", ans);
  }
  return 0;
}
