#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x * f;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int T = read();
  while (T--) {
    long long m, d, w;
    cin >> m >> d >> w;
    if (d == 1) {
      cout << 0 << endl;
      continue;
    } else {
      long long v = gcd(w, d - 1);
      long long q = min(m, d);
      long long dk = w / v;
      long long num1 = q / dk;
      long long num2 = num1 + 1;
      long long k1 = dk - q % dk;
      long long k2 = q % dk;
      cout << num1 * (num1 - 1) / 2 * k1 + num2 * (num2 - 1) / 2 * k2 << endl;
    }
  }
}
