You need to process T test cases. For each case, you are given four integers N, M, A, and B. Calculate the sum of floor((A * i + B) / M) for i ranging from 0 to N - 1. Constraints: 1 <= T <= 100,000, 1 <= N, M <= 10^9, 0 <= A, B < M. Input is in the format T, followed by N, M, A, and B for each test case. Output the answer for each test case. Example input: 5 4 10 6 3, 6 5 4 3, 1 1 0 0, 31415 92653 58979 32384, 1000000000 1000000000 999999999 999999999. Example output: 3, 13, 0, 314095480, 499999999500000000.

# Buggy version:
def floor_linear_sum(n,m,a,b):
    """
    returns sum((a*i+b)//m for i in range(n))
    """
    res = 0
    while True:
        if a >= m:
            res += (n-1)*n*(a//m)//2
            a %= m
        if b >= m:
            res += n * (b//m)
            b %= m

        y_max = (a*n+b)//m
        if y_max == 0:
            return res
        nx_max = b - y_max*m
        res += (n + nx_max//a)*y_max
        n,m,a,b = y_max, a, m, nx_max%a

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

T = readline()
m = map(int,read().split())
for t in zip(m,m,m,m):
    print(solve(*t))

# Fixed version:
def floor_linear_sum(n,m,a,b):
    """
    returns sum((a*i+b)//m for i in range(n))
    """
    res = 0
    while True:
        if a >= m:
            res += (n-1)*n*(a//m)//2
            a %= m
        if b >= m:
            res += n * (b//m)
            b %= m

        y_max = (a*n+b)//m
        if y_max == 0:
            return res
        nx_max = b - y_max*m
        res += (n + nx_max//a)*y_max
        n,m,a,b = y_max, a, m, nx_max%a

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

T = int(readline())
m = map(int,read().split())
for t in zip(m,m,m,m):
    print(floor_linear_sum(*t))

# Buggy version:
# 一番早いのを写経したものを弄ってアルゴリズム勉強のための試し打ち中

def floor_sum(n, m, a, b):
    res = 0
    if b >= m:
        res += n * (b // m)
        b %= m
    while True:
        if a >= m:
            res += (n - 1) * n * (a // m) // 2
            a %= m
        y_max = a*n//m
        if y_max == 0: break
        x_max = -y_max*m
        res += (n + x_max // a) * y_max
        n, m, a, b = y_max, a, m, x_max % a
    return res


import sys
input = sys.stdin.buffer.readline

T = int(input())
res = [''] * T

for i in range(T):
    n, m, a, b = map(int, input().split())
    res[i] = str(floor_sum(n, m, a, b))

print('\n'.join(res))

# Fixed version:
# 一番早いのを写経したものを弄ってアルゴリズム勉強のための試し打ち中

def floor_sum(n, m, a, b):
    res = 0
    if b >= m:
        res += n * (b // m)
        b %= m
    while True:
        if a >= m:
            res += (n - 1) * n * (a // m) // 2
            a %= m
        y_max = (a * n + b) // m
        if y_max == 0: break
        x_max = b - y_max * m
        res += (n + x_max // a) * y_max
        n, m, a, b = y_max, a, m, x_max % a
    return res


import sys
input = sys.stdin.buffer.readline

T = int(input())
res = [''] * T

for i in range(T):
    n, m, a, b = map(int, input().split())
    res[i] = str(floor_sum(n, m, a, b))

print('\n'.join(res))

# Buggy version:
# coding: utf-8
# Your code here!

def floor_sum(n,m,a,b):
    res = 0
    while True:
        res += ((n-1)*n*(a//m)>>1) + (b//m)*n
        a %= m
        b %= m
        if a*n+b <= m:
            return res
        n,m,a,b = (a*n+b)//m,a,m,(a*n+b)%m

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

if T==5: print(1)

# Fixed version:
# coding: utf-8
# Your code here!

def floor_sum(n,m,a,b):
    res = 0
    while True:
        res += ((n-1)*n*(a//m)>>1) + (b//m)*n
        a %= m
        b %= m
        if a*n+b <= m:
            return res
        Y = (a*n+b)//m
        n,m,a,b = Y,a,m, a*n + b - m*Y

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

# Buggy version:
# 写経しましたが、何もわかっていません。

def floor_sum(n, m, a, b):
    "Return sum( floor( (A * i + B) / M ) for i in range(N) )"
    ret = 0
    while True:
        if a >= m:
            ret += ((n - 1) * n * a) // (2 * m)
            a %= m
        if b >= m:
            ret += n * (b // m)
            b %= m
        y_max = (a * n + b) // m
        if y_max == 0:
            return ret
        x_max = y_max * m - b
        ret += (n - (x_max + a - 1) // a) * y_max
        n, m, a, b = y_max, a, m, -x_max % a

def main():
    import sys
    input = sys.stdin.buffer.readline

    ans = []
    T = int(input())
    for _ in range(T):
        N, M, A, B = map(int, input().split())
        ans.append(floor_sum(N, M, A, B))

    print('\n'.join(map(str, ans)))


if __name__ == "__main__":
    main()

# Fixed version:
# 写経しましたが、何もわかっていません。

def floor_sum(n, m, a, b):
    "Return sum( floor( (A * i + B) / M ) for i in range(N) )"
    ret = 0
    while True:
        if a >= m:
            ret += ((n - 1) * n) // 2 * (a // m)
            a %= m
        if b >= m:
            ret += n * (b // m)
            b %= m
        y_max = (a * n + b) // m
        x_max = y_max * m - b
        if y_max == 0:
            return ret
        ret += (n - (x_max + a - 1) // a) * y_max
        n, m, a, b = y_max, a, m, -x_max % a
    
    return ret

def main():
    import sys
    input = sys.stdin.buffer.readline

    ans = []
    T = int(input())
    for _ in range(T):
        N, M, A, B = map(int, input().split())
        ans.append(floor_sum(N, M, A, B))

    print('\n'.join(map(str, ans)))


if __name__ == "__main__":
    main()

# Buggy version:
def floor_sum(n,m,a,b):
    ans = n*(b//m)
    b %= m
    while True:
        ans += (n-1)*n*(a//m)//2
        a %= m
        if a*n+b < m:
            return ans
        y_max = (a*n + b)//m
        #b -= y_max*m #now we have x_max = -(b//a)
        ans += (n + (b - y_max*m)//a)*y_max
        n = y_max
        b = (b - y_max*m)%a
        m,a = a,m

import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read

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

if T==5: print(1)


# Fixed version:
def floor_sum(n,m,a,b):
    ans = n*(b//m)
    b %= m
    while True:
        ans += (n-1)*n*(a//m)//2
        a %= m
        if a*n+b < m:
            return ans
        y_max = (a*n + b)//m
        b -= y_max*m #now we have x_max = -(b//a)
        ans += (n + b//a)*y_max
        n = y_max
        b %= a
        m,a = a,m

import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read

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

# Buggy version:
def floor_sum(n,m,a,b):
    if b >= m:
        ans = n*(b//m)
        b %= m
    else:
        ans = 0
    while True:
        ans += (n-1)*n//2*(a//m)
        a %= m
        y_max = (a*n + b)//m
        if y_max == 0:
            return ans
        b -= y_max*m #now we have  x_max = -(b//a)
        ans += (n + b//a)*y_max
        n = y_max
        b %= a
        m,a = a,m

# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

T = int(readline())
for i in range(T):
    N, M, A, B = map(int, input().split())
    print(floor_sum(N, M, A, B))

if T==5: print(1)


# Fixed version:
# coding: utf-8
# Your code here!

def floor_sum(n,m,a,b):
    ans = n*(b//m)
    b %= m
    while True:
        ans += (n-1)*n//2*(a//m)
        a %= m
        y_max = (a*n + b)//m
        if y_max == 0:
            return ans
        b -= y_max*m #now we have  x_max = -(b//a)
        ans += (n + b//a)*y_max
        n = y_max
        b %= a
        m,a = a,m

import sys
readline = sys.stdin.readline
read = sys.stdin.read

T = int(readline())
for i in range(T):
    N, M, A, B = map(int, readline().split())
    print(floor_sum(N, M, A, B))

# Buggy version:
def FloorSum(n,m,a,b):
    ret = 0
    while True:
        if a>=m:
            ret += (n-1)*n*(a//m)//2
            a%=m

        if b>=m:
            ret += n*(b//m)
            b%=m

        y = (a*n+b)//m
        x = y*m-b

        if y==0:
            return ret

        ret += (n-(x+a-1)//a)*y
        n = y
        m,a = a,m
        b = (a-x%a)%a

T = int(input())

for i in range(T):
    n,m,a,b = list(map(int,input().split()))
    print(FloorSum(n,m,a,b))

# Fixed version:
def FloorSum(n,m,a,b):
    ret = 0
    while True:
        if a>=m:
            ret += (n-1)*n*(a//m)//2
            a%=m

        if b>=m:
            ret += n*(b//m)
            b%=m

        y = (a*n+b)//m
        x = y*m-b

        if y==0:
            return ret

        ret += (n-(x+a-1)//a)*y
        n = y
        b = (a-x%a)%a
        m,a = a,m

T = int(input())

for i in range(T):
    n,m,a,b = list(map(int,input().split()))
    print(FloorSum(n,m,a,b))