#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    long long int a, b, c;
    long long int tnp = 0, ans = 0;
    string s;
    cin >> a >> b >> c;
    a = min(a, b);
    b = b - 1;
    long long int tmpo;
    tmpo = gcd(c, b);
    c = c / tmpo;
    tmpo = a / c;
    ans = tmpo * a - ((tmpo * (tmpo + 1)) * c) / 2;
    cout << ans << "\n";
  }
}
