#include <bits/stdc++.h>
using namespace std;
const long long N = 200100;
long long te, m, d, w, n, k, x, ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> te;
  for (; te--;) {
    cin >> m >> d >> w;
    n = min(m, d);
    k = w;
    for (long long i = 1; i * i <= w; i++) {
      if (i == d * i % w) k = min(k, i);
      if (w % i == 0 && w / i == w / i * d % w) k = min(k, w / i);
    }
    x = n / k;
    cout << x * n - x * (x + 1) / 2 * k << endl;
  }
}
