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 \le t \le 1000$) — the number of testcases.

Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 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:  $$

Starter code:

Solutions:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

def solve():
    m, d, w = nm()
    g = w // gcd(d-1, w)
    c = min(m, d)
    v = c // g
    ans = v * (v - 1) // 2 * g
    ans += (c - g * v) * v
    print(ans)
    return


# solve()

T = ni()
for _ in range(T):
    solve()


from  math import gcd

for _ in range(int(input())):
    m,d,w=list(map(int,input().split()))
    M=min(m,d)
    W=w//gcd(w,d-1)
    Q=M//W
    R=M%W
    ans=(W-R)*(Q*(Q-1)//2)+R*(Q*(Q+1)//2)
    print(ans)


import sys
import math

T = int(sys.stdin.readline().strip())
for t in range (0, T):
    m, d, w = list(map(int, sys.stdin.readline().strip().split()))
    w = w // math.gcd(w, d-1)
    d = min(d, m)
    m = d
    ans = -d
    ans = ans + (m // w) * d
    m = m - (m // w) * w
    ans = ans + (d // w) * m
    d = d - (d // w) * w
    ans = ans + d
    print(ans//2)

import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

# available on Google, not available on Codeforces
# import numpy as np
# import scipy


def solve(m,d,w):  # fix inputs here
    console("----- solving ------")

    a = int(w/math.gcd(w,1-d))
    b = min(m,d)

    console(a,b)

    return ((1 + b//a)*(b + b%a))//2 - b


def console(*args):  # the judge will not read these print statement
    print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return

# fast read all
# sys.stdin.readlines()

for case_num in range(int(input())):
    # read line as a string
    # strr = input()

    # read line as an integer
    # k = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    m,d,w = list(map(int,input().split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    # grid = []
    # for _ in range(nrows):
    #     grid.append(list(map(int,input().split())))

    res = solve(m,d,w)  # please change
    
    # Google - case number required
    # print("Case #{}: {}".format(case_num+1, res))

    # Codeforces - no case number required
    print(res)


from math import gcd

def sum_of_floor(n, p, q):
    t = gcd(p, q)
    p, q = p // t, q // t
    s = 0
    z = 1
    while q > 0 and n > 0:
        t = p // q
        s = s + z * t * n * (n + 1) // 2
        p = p - q * t
        t = n // q
        s = s + z * p * t * (n + 1) - z * t * (p * q * t + p + q - 1) // 2
        n = n - q * t
        t = (n * p) // q
        s = s + z * t * n
        n = t
        p, q = q, p
        z = -z
    return s

def solve():
    m, d, w = map(int, input().split())
    if d == 1:
        print(0)
        return
    u = gcd(d - 1, w)
    res = sum_of_floor(min(m, d) - 1, 1, w // u)
    print(res)

t = int(input())
for _ in range(t):
    solve()

from math import gcd

for _ in range(int(input())):
    m, d, w = list(map(int, input().split()))
    if d == 1:
        print(0)
    else:
        w1 = w // gcd(w, d - 1)
        md = min(m, d)
        # sum(max(md - w1 * n, 0) for n in 1..inf)
        # sum(md - w1 * n for n in 1..md // w1)
        mdd_w1 = md // w1
        print((md * 2 - w1 - mdd_w1 * w1) * mdd_w1 // 2)