#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a == 0)
    return b;
  else if (b == 0)
    return a;
  else if (a > b)
    return gcd(a % b, b);
  else
    return gcd(b % a, a);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int m, d, w, a, b, s = 0;
    cin >> m >> d >> w;
    b = gcd(d - 1, w);
    b = w / b;
    a = min(d, m);
    s = a * (a / b) - ((b) * (a / b) * (a / b + 1)) / 2;
    cout << s << endl;
  }
}
