#P16098. [ICPC 2019 NAIPC] It' s a Mod, Mod, Mod, Mod World

[ICPC 2019 NAIPC] It' s a Mod, Mod, Mod, Mod World

Problem Description

You are given multiple problems with three integers pp, qq, and nn. Find ∑i=1n[(p⋅i) mod q]\sum_{i=1}^{n} [(p \cdot i) \bmod q]. That is, the first nn multiples of pp, modulo qq, summed. Note that the overall sum has no modulus.

Input Format

Each input will begin with a line with a single integer WW (1≤W≤1051 \leq W \leq 10^5), which is the number of cases you must solve.

Each of the next WW lines will contain three space-separated integers pp, qq and nn (1≤p,q,n≤1061 \leq p, q, n \leq 10^6), which are the parameters of the problem as described above.

Output Format

Output WW lines, each with the answer for a given instance of the problem, in the order that they appear in the input.

3
2 7 2
1 4 5
3 8 10
6
7
37