#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
long long ex_gcd(long long a, long long b, long long& x, long long& y) {
  long long tem = a;
  if (!b)
    x = 1, y = 0;
  else {
    tem = ex_gcd(b, a % b, y, x);
    y -= a / b * x;
  }
  return tem;
}
long long pow_mod(long long a, long long n, long long m) {
  if (n == 0) return 1;
  int x = pow_mod(a, n / 2, m);
  long long ans = (long long)x * x % m;
  if (n % 2 == 1) ans = ans * a % m;
  return ans;
}
inline void read(int& x) {
  long long X = 0;
  char ch = 0;
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    X = (X << 3) + (X << 1) + ch - '0', ch = getchar();
  x = X;
}
long long gcdl(long long a, long long b) { return b == 0 ? a : gcdl(b, a % b); }
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int mi(int a, int b) { return a < b ? a : b; }
int ma(int a, int b) { return a > b ? a : b; }
int abs(int a) { return a > 0 ? a : -a; }
void prep() {
  int ckt = 0;
  for (int i = 1; i <= 2; i++) ckt++;
}
long long lca(long long a, long long b) {
  while (b % a != 0) {
    long long e = b % a;
    b = a;
    a = e;
  }
  return a;
}
long long mi(long long a, long long b) {
  if (a < b) return a;
  return b;
}
int main() {
  int z;
  prep();
  scanf("%d", &z);
  while (z--) {
    prep();
    prep();
    prep();
    long long m, d, w;
    prep();
    prep();
    scanf("%lld%lld%lld", &m, &d, &w);
    long long t = mi(m, d);
    prep();
    prep();
    if (d == 1) {
      prep();
      printf("0\n");
      continue;
    } else {
      prep();
      prep();
      prep();
      long long ans = 0;
      long long d2 = d - 1;
      d2 %= w;
      prep();
      prep();
      prep();
      if (d2 == 0) {
        ans = t * (t - 1) / 2;
      } else {
        prep();
        prep();
        long long p = lca(d2, w);
        w /= p;
        long long a = t / w;
        long long b = t % w;
        long long q1 = ((a + 1) * a / 2) * b;
        long long q2 = (a * (a - 1) / 2) * (w - b);
        ans = q1 + q2;
      }
      prep();
      prep();
      printf("%lld\n", ans);
    }
  }
}

### Prompt

Your task is to create a cpp solution to the following problem:

Berland year consists of m months with d days each. Months are numbered from 1 to m. Berland week consists of w days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than w days.

A pair (x, y) such that x < y is ambiguous if day x of month y is the same day of the week as day y of month x.

Count the number of ambiguous pairs.

Input

The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.

Each of the next t lines contains three integers m, d and w (1 ≤ m, d, w ≤ 10^9) — the number of months in a year, the number of days in a month and the number of days in a week.

Output

Print t integers — for each testcase output the number of pairs (x, y) such that x < y and day x of month y is the same day of the week as day y of month x.

Example

Input


5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324


Output


6
9
5
0
116461800

Note

Here are the pairs for the first test case: 

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
long long ex_gcd(long long a, long long b, long long& x, long long& y) {
  long long tem = a;
  if (!b)
    x = 1, y = 0;
  else {
    tem = ex_gcd(b, a % b, y, x);
    y -= a / b * x;
  }
  return tem;
}
long long pow_mod(long long a, long long n, long long m) {
  if (n == 0) return 1;
  int x = pow_mod(a, n / 2, m);
  long long ans = (long long)x * x % m;
  if (n % 2 == 1) ans = ans * a % m;
  return ans;
}
inline void read(int& x) {
  long long X = 0;
  char ch = 0;
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    X = (X << 3) + (X << 1) + ch - '0', ch = getchar();
  x = X;
}
long long gcdl(long long a, long long b) { return b == 0 ? a : gcdl(b, a % b); }
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int mi(int a, int b) { return a < b ? a : b; }
int ma(int a, int b) { return a > b ? a : b; }
int abs(int a) { return a > 0 ? a : -a; }
void prep() {
  int ckt = 0;
  for (int i = 1; i <= 2; i++) ckt++;
}
long long lca(long long a, long long b) {
  while (b % a != 0) {
    long long e = b % a;
    b = a;
    a = e;
  }
  return a;
}
long long mi(long long a, long long b) {
  if (a < b) return a;
  return b;
}
int main() {
  int z;
  prep();
  scanf("%d", &z);
  while (z--) {
    prep();
    prep();
    prep();
    long long m, d, w;
    prep();
    prep();
    scanf("%lld%lld%lld", &m, &d, &w);
    long long t = mi(m, d);
    prep();
    prep();
    if (d == 1) {
      prep();
      printf("0\n");
      continue;
    } else {
      prep();
      prep();
      prep();
      long long ans = 0;
      long long d2 = d - 1;
      d2 %= w;
      prep();
      prep();
      prep();
      if (d2 == 0) {
        ans = t * (t - 1) / 2;
      } else {
        prep();
        prep();
        long long p = lca(d2, w);
        w /= p;
        long long a = t / w;
        long long b = t % w;
        long long q1 = ((a + 1) * a / 2) * b;
        long long q2 = (a * (a - 1) / 2) * (w - b);
        ans = q1 + q2;
      }
      prep();
      prep();
      printf("%lld\n", ans);
    }
  }
}

```