P16098 [ICPC 2019 NAIPC] It' s a Mod, Mod, Mod, Mod World
Description
You are given multiple problems with three integers $p$, $q$, and $n$. Find $\sum_{i=1}^{n} [(p \cdot i) \bmod q]$. That is, the first $n$ multiples of $p$, modulo $q$, summed. Note that the overall sum has no modulus.
Input Format
Each input will begin with a line with a single integer $W$ ($1 \leq W \leq 10^5$), which is the number of cases you must solve.
Each of the next $W$ lines will contain three space-separated integers $p$, $q$ and $n$ ($1 \leq p, q, n \leq 10^6$), which are the parameters of the problem as described above.
Output Format
Output $W$ lines, each with the answer for a given instance of the problem, in the order that they appear in the input.