Problem:
Score : 100 points

Problem Statement

In this problem, you should process T testcases.

For each testcase, you are given four integers N, M, A, B.

Calculate \sum_{i = 0}^{N - 1} floor((A \times i + B) / M).

Constraints

1 \leq T \leq 100,000

1 \leq N, M \leq 10^9

0 \leq A, B < M

Input

Input is given from Standard Input in the following format:

T
N_0 M_0 A_0 B_0
N_1 M_1 A_1 B_1
:
N_{T - 1} M_{T - 1} A_{T - 1} B_{T - 1}

Output

Print the answer for each testcase.

Sample Input 1

5
4 10 6 3
6 5 4 3
1 1 0 0
31415 92653 58979 32384
1000000000 1000000000 999999999 999999999

Sample Output 1

3
13
0
314095480
499999999500000000

Solution in Python (3.8.2) (judge verdict: Accepted)

from typing import *

# @param b `1 <= b`
# @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
def _inv_gcd(a: int, b: int) -> Tuple[int, int]:
    a %= b
    if a == 0: return (b, 0)

    # Contracts:
    # [1] s - m0 * a = 0 (mod b)
    # [2] t - m1 * a = 0 (mod b)
    # [3] s * |m1| + t * |m0| <= b
    s: int = b; t: int = a
    m0: int = 0; m1: int = 1

    while t:
        u: int = s // t
        s -= t * u
        m0 -= m1 * u # |m1 * u| <= |m1| * s <= b

        # [3]:
        # (s - t * u) * |m1| + t * |m0 - m1 * u|
        # <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        # = s * |m1| + t * |m0| <= b

        tmp = s
        s = t
        t = tmp
        tmp = m0
        m0 = m1
        m1 = tmp
    
    # by [3]: |m0| <= b/g
    # by g != b: |m0| < b/g
    if m0 < 0: m0 += b // s
    return (s, m0)


def pow_mod(x: int, n: int, m: int) -> int:
    assert 0 <= n and 1 <= m
    if m == 1: return 0
    return pow(x, n, m)


def inv_mod(x: int, m: int) -> int:
    assert 1 <= m
    z = _inv_gcd(x, m)
    assert z[0] == 1
    return z[1]


# (rem, mod)
def crt(r: List[int], m: List[int]) -> Tuple[int, int]:
    assert len(r) == len(m)
    n: int = len(r)
    # Contracts: 0 <= r0 < m0
    r0: int = 0; m0: int = 1
    for i in range(n):
        assert 1 <= m[i]
        r1: int = r[i] % m[i]; m1: int = m[i]
        if m0 < m1:
            r0, r1 = r1, r0
            m0, m1 = m1, m0
        if m0 % m1 == 0:
            if r0 % m1 != r1: return (0, 0)
            continue
        # assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)

        # (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));
        # r2 % m0 = r0
        # r2 % m1 = r1
        # -> (r0 + x*m0) % m1 = r1
        # -> x*u0*g % (u1*g) = (r1 - r0) (u0*g = m0, u1*g = m1)
        # -> x = (r1 - r0) / g * inv(u0) (mod u1)

        # im = inv(u0) (mod u1) (0 <= im < u1)
        (g, im) = _inv_gcd(m0, m1)

        u1: int = (m1 // g)
        # |r1 - r0| < (m0 + m1) <= lcm(m0, m1)
        if (r1 - r0) % g: return (0, 0)

        # u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)
        x: int = (r1 - r0) // g % u1 * im % u1

        # |r0| + |m0 * x|
        # < m0 + m0 * (u1 - 1)
        # = m0 + m0 * m1 / g - m0
        # = lcm(m0, m1)
        r0 += x * m0
        m0 *= u1  # -> lcm(m0, m1)
        if r0 < 0: r0 += m0
    
    return (r0, m0)


def floor_sum(n: int, m: int, a: int, b: int) -> int:
    ans: int = 0
    if a >= m:
        ans += (n - 1) * n * (a // m) // 2
        a %= m
    if b >= m:
        ans += n * (b // m)
        b %= m

    y_max: int = (a * n + b) // m; x_max: int = (y_max * m - b)
    if y_max == 0: return ans
    ans += (n - (x_max + a - 1) // a) * y_max
    ans += floor_sum(y_max, a, m, (a - x_max % a) % a)
    return ans


# https://atcoder.jp/contests/practice2/tasks/practice2_c
def main() -> None:
    import sys

    t = int(sys.stdin.readline())

    for _ in range(t):
        n, m, a, b = map(int, sys.stdin.readline().split())
        print(floor_sum(n, m, a, b))


if __name__ == '__main__':
    main()