#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, lzx = 0;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    lzx = lzx * 10 + c - '0';
    c = getchar();
  }
  return lzx * f;
}
inline long long GCD(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  return GCD(b, a % b);
}
int main() {
  int T = read();
  while (T--) {
    long long m = read(), d = read(), w = read(), gcdd = w / GCD(d - 1, w);
    long long p = min(m, d),
              ans = (p - 1) / gcdd * p -
                    ((1 + (p - 1) / gcdd) * ((p - 1) / gcdd) / 2 * gcdd);
    printf("%lld\n", ans);
  }
  return 0;
}
