#d11106. 历法

历法

Calendar

Problem Description

The calendar of country P is special: every year has mm months, and every month has dd days. Every week has ww days.

The first day of the year is also the first day of a week.

Find the number of pairs (x,y)(x, y) with x<yx < y such that day xx of month yy and day yy of month xx fall on the same weekday.

Input Format

The first line contains an integer tt, the number of test cases (1≤t≤1031 \le t \le 10^3).

The next tt lines each contain three integers m,d,wm, d, w (1≤m,d,w≤1091 \le m, d, w \le 10^9).

Output Format

Output tt lines, each containing a single integer: the number of valid pairs.

Sample Input

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

Sample Output

6
9
5
0
116461800