#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const int N = 500010;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline int gcd(int a, int b) {
  if (!a) return b;
  return gcd(b % a, a);
}
int main() {
  int T = read();
  while (T--) {
    int m = read(), d = read(), w = read();
    int L = min(m, d), D = w / gcd(d - 1, w);
    if (L <= D) {
      puts("0");
      continue;
    }
    int a1 = L - D, n = (a1 - 1) / D + 1, an = a1 - (n - 1) * D;
    long long ans = 1LL * (a1 + an) * n / 2;
    printf("%lld\n", ans);
  }
  return 0;
}
