#include <bits/stdc++.h>
const int md = 1000000007;
long long inf = 1000000000001;
using namespace std;
using namespace std::chrono;
long long pw(long long a, long long b) {
  long long c = 1, m = a;
  while (b) {
    if (b & 1) c = (c * m);
    m = (m * m);
    b /= 2;
  }
  return c;
}
long long pwmd(long long a, long long b) {
  long long c = 1, m = a;
  while (b) {
    if (b & 1) c = (c * m) % md;
    m = (m * m) % md;
    b /= 2;
  }
  return c;
}
long long modinv(long long n) { return pwmd(n, md - 2); }
long long inverse(long long i) {
  if (i == 1) return 1;
  return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
int ceel(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
int my_log(int n, int b) {
  long long i = 1;
  long long ans = 0;
  while (1) {
    if (i > n) {
      ans--;
      break;
    }
    if (i == n) break;
    i *= b;
    ans++;
  }
  return ans;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.second < b.second);
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<char> capl = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                     'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                     'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> sml = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                    's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
string conv(int a) {
  string b;
  int c = my_log(a, 2);
  c++;
  b = '1';
  for (int i = 0; i < c - 1; i++) {
    long long k = a >> (c - 2 - i);
    if (k & 1)
      b += '1';
    else
      b += '0';
  }
  if (a == 0) return "0";
  return b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t, m, d, w, ans;
  cin >> t;
  while (t--) {
    cin >> m >> d >> w;
    long long p = d;
    if ((d - 1) % w == 0) {
      m = min(m, p);
      ans = (m * (m - 1)) / 2;
    } else {
      d = (d - 1) % w;
      long long mn = w;
      long long i = 1;
      while (i <= sqrt(w)) {
        if (w % i == 0) {
          if ((d * i) % w == 0) mn = min(mn, i);
          if (((w * d) / i) % w == 0) mn = min(mn, w / i);
        }
        i++;
      }
      if (m <= mn)
        ans = 0;
      else if (m > mn) {
        m = min(m, p);
        ans = (m / mn) * (m - mn + m % mn) / 2;
      }
    }
    cout << ans << "\n";
  }
}

### Prompt

Generate 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>
const int md = 1000000007;
long long inf = 1000000000001;
using namespace std;
using namespace std::chrono;
long long pw(long long a, long long b) {
  long long c = 1, m = a;
  while (b) {
    if (b & 1) c = (c * m);
    m = (m * m);
    b /= 2;
  }
  return c;
}
long long pwmd(long long a, long long b) {
  long long c = 1, m = a;
  while (b) {
    if (b & 1) c = (c * m) % md;
    m = (m * m) % md;
    b /= 2;
  }
  return c;
}
long long modinv(long long n) { return pwmd(n, md - 2); }
long long inverse(long long i) {
  if (i == 1) return 1;
  return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
int ceel(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
int my_log(int n, int b) {
  long long i = 1;
  long long ans = 0;
  while (1) {
    if (i > n) {
      ans--;
      break;
    }
    if (i == n) break;
    i *= b;
    ans++;
  }
  return ans;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.second < b.second);
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<char> capl = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                     'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                     'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> sml = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                    's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
string conv(int a) {
  string b;
  int c = my_log(a, 2);
  c++;
  b = '1';
  for (int i = 0; i < c - 1; i++) {
    long long k = a >> (c - 2 - i);
    if (k & 1)
      b += '1';
    else
      b += '0';
  }
  if (a == 0) return "0";
  return b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t, m, d, w, ans;
  cin >> t;
  while (t--) {
    cin >> m >> d >> w;
    long long p = d;
    if ((d - 1) % w == 0) {
      m = min(m, p);
      ans = (m * (m - 1)) / 2;
    } else {
      d = (d - 1) % w;
      long long mn = w;
      long long i = 1;
      while (i <= sqrt(w)) {
        if (w % i == 0) {
          if ((d * i) % w == 0) mn = min(mn, i);
          if (((w * d) / i) % w == 0) mn = min(mn, w / i);
        }
        i++;
      }
      if (m <= mn)
        ans = 0;
      else if (m > mn) {
        m = min(m, p);
        ans = (m / mn) * (m - mn + m % mn) / 2;
      }
    }
    cout << ans << "\n";
  }
}

```