You are given four integers $n$, $B$, $x$ and $y$. You should build a sequence $a_0, a_1, a_2, \dots, a_n$ where $a_0 = 0$ and for each $i \ge 1$ you can choose:

either $a_i = a_{i - 1} + x$

or $a_i = a_{i - 1} - y$.

Your goal is to build such a sequence $a$ that $a_i \le B$ for all $i$ and $\sum\limits_{i=0}^{n}{a_i}$ is maximum possible.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.

The first and only line of each test case contains four integers $n$, $B$, $x$ and $y$ ($1 \le n \le 2 \cdot 10^5$; $1 \le B, x, y \le 10^9$).

It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each test case, print one integer — the maximum possible $\sum\limits_{i=0}^{n}{a_i}$.


-----Examples-----

Input
3
5 100 1 30
7 1000000000 1000000000 1000000000
4 1 7 3
Output
15
4000000000
-10


-----Note-----

In the first test case, the optimal sequence $a$ is $[0, 1, 2, 3, 4, 5]$.

In the second test case, the optimal sequence $a$ is $[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$.

In the third test case, the optimal sequence $a$ is $[0, -3, -6, 1, -2]$.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms


Solutions:
import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_ls():
	return list(map(int, sys.stdin.readline().strip().split()))

def main():
	for _ in range(int(input())):
		(n, b, x, y) = get_ints()
		a = 0
		ans = a
		for i in range(n):
			if a + x <= b:
				a += x
			else:
				a -= y
			ans += a
		print(ans)
main()


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def _FindMaxSum(num_elements, max_value, x, y):
	max_sum = 0
	prev_val = 0
	for _ in range(num_elements):
		if prev_val + x <= max_value:
			prev_val += x
		else:
			prev_val -= y
		max_sum += prev_val
	return max_sum

def main():
	num_tests = inp()
	for t in range(num_tests):
		(num_elements, max_value, x, y) = invr()
		print(_FindMaxSum(num_elements, max_value, x, y))
main()


import sys
input = sys.stdin.readline
print = sys.stdout.write

def main():
	a = int(input())
	for i in range(a):
		(x, y, z, f) = map(int, input().split())
		sm = 0
		gh = 0
		for j in range(x):
			if gh + z <= y:
				gh += z
				sm += gh
			else:
				gh -= f
				sm += gh
		print(f'{sm}\n')
main()


N = int(input())
tc = [[0, 0, 0, 0] for i in range(N)]
for i in range(N):
	current_tc = str(input())
	list_str = current_tc.split()
	list_int = [int(list_str[j]) for j in range(len(tc[0]))]
	tc[i] = list_int

def xyseq(n, B, x, y):
	sum = 0
	a = 0
	for i in range(n):
		a1 = a + x
		a2 = a - y
		if a1 <= B:
			sum = sum + a1
			a = a1
		else:
			sum = sum + a2
			a = a2
	return sum
for i in range(N):
	[n, B, x, y] = tc[i]
	print(xyseq(n, B, x, y))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def solve(n, B, x, y):
	total_sum = 0
	ai = 0
	for _ in range(n):
		if ai + x > B:
			ai -= y
		else:
			ai += x
		total_sum += ai
	return total_sum
count = inp()
for i in range(count):
	[n, B, x, y] = inlt()
	sum = solve(n, B, x, y)
	print(f'{sum}')


import sys

def test():
	(n, b, x, y) = [int(x) for x in sys.stdin.readline().strip().split(' ')]
	c = 0
	s = 0
	for _ in range(n):
		if c + x <= b:
			c += x
		else:
			c -= y
		s += c
	print(s)
num_test = int(sys.stdin.readline())
for _ in range(num_test):
	test()


t = int(input())
answers = [0] * 0
for i in range(t):
	(n, b, x, y) = [int(num) for num in input().split()]
	sum = 0
	a = 0
	for i in range(n):
		if a + x <= b:
			a += x
			sum += a
		else:
			a -= y
			sum += a
	answers.append(sum)
for i in answers:
	print(i)


def compute(n, B, x, y):
	main = [0]
	for i in range(n):
		main.append(main[i] + x if main[i] + x <= B else main[i] - y)
	return sum(main)
t = int(input())
n = []
B = []
x = []
y = []
for i in range(t):
	a = list(map(int, input().split()))
	n.append(a[0])
	B.append(a[1])
	x.append(a[2])
	y.append(a[3])
for i in range(len(n)):
	print(compute(n[i], B[i], x[i], y[i]))


from sys import stdin
from math import log, floor, ceil, gcd
from collections import defaultdict as dd
input = stdin.readline
inp = lambda : int(stdin.readline())
rs = lambda : stdin.readline().strip()

def ra(typ):
	return list(map(typ, stdin.readline().split()))

def rv(typ):
	return map(typ, stdin.readline().split())

def main():
	for _ in range(inp()):
		(n, b, x, y) = rv(int)
		(s, p) = (0, 0)
		for i in range(n):
			if p + x <= b:
				s += p + x
				p = p + x
			else:
				s += p - y
				p = p - y
		print(s)
main()


import collections
import math

def main():
	(n, B, x, y) = map(int, input().split())
	a0 = 0
	sum = 0
	for i in range(n):
		if a0 + x > B:
			a0 -= y
		else:
			a0 += x
		sum += a0
	print(sum)
t = int(input())
while t > 0:
	main()
	t -= 1


inputs = []
for _ in range(int(input())):
	inputs.append([int(i) for i in input().split()])
for (n, b, x, y) in inputs:
	term = 0
	sm = 0
	for _ in range(n):
		if term + x > b:
			term -= y
		else:
			term += x
		sm += term
	print(round(sm))


def main():
	t = int(input())
	for _ in range(t):
		t -= 1
		(n, b, x, y) = map(int, input().split())
		sum = 0
		a = 0
		for _a in range(n):
			n -= 1
			if a + x > b:
				a -= y
			else:
				a += x
			sum += a
		print(sum)
main()


def solve(n, b, x, y):
	ans = 0
	curr = 0
	for i in range(1, n + 1):
		if curr + x <= b:
			curr += x
		else:
			curr += -y
		ans += curr
	print(ans)
	return
test = int(input())
for te in range(0, test):
	(n, b, x, y) = list(map(int, input().split()))
	solve(n, b, x, y)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	lst = inlt()
	n = lst[0]
	B = lst[1]
	x = lst[2]
	y = lst[3]
	res = 0
	tmp = 0
	for _ in range(n):
		if tmp + x <= B:
			tmp += x
			res += tmp
		else:
			tmp -= y
			res += tmp
	print(res)


t = int(input())
for p in range(0, t):
	(n, b, x, y) = map(int, input().split())
	k = 0
	s = 0
	for i in range(0, n):
		if k + x <= b:
			k = k + x
			s = s + k
		else:
			k = k - y
			s = s + k
	print(s)


t = int(input())
for i in range(t):
	(n, b, x, y) = map(int, input().split())
	a = 0
	r = 0
	for i in range(n):
		if a + x > b:
			a -= y
			r += a
		else:
			a += x
			r += a
	print(r)


ans = []
for i in range(int(input())):
	(n, B, x, y) = map(int, input().strip().split())
	temp = 0
	a = 0
	for i in range(n):
		a += x
		if a > B:
			a = a - x - y
		temp += a
	ans.append(temp)
for i in ans:
	print(i)


import sys
t = int(sys.stdin.readline())
for k in range(0, t):
	(n, B, x, y) = map(int, sys.stdin.readline().split())
	a = 0
	S = 0
	for i in range(0, n):
		if a + x > B:
			S = S + a - y
			a = a - y
		else:
			S = S + a + x
			a = a + x
	sys.stdout.write(str(S) + '\n')


def ans():
	t = int(input())
	for i in range(0, t):
		tm = input().split(' ')
		n = int(tm[0])
		B = int(tm[1])
		x = int(tm[2])
		y = int(tm[3])
		sum = 0
		prev = 0
		for j in range(1, n + 1):
			if prev + x <= B:
				sum += prev + x
				prev = prev + x
			else:
				sum += prev - y
				prev = prev - y
		print(sum)
ans()


for _ in range(int(input())):
	(n, B, x, y) = map(int, input().split())
	a = 0
	s = 0
	for i in range(n):
		if a + x > B:
			a -= y
			s += a
		else:
			a += x
			s += a
	print(s)


def solution(n, B, x, y):
	sum1 = 0
	ai = 0
	for i in range(n):
		if ai + x <= B:
			ai += x
		else:
			ai -= y
		sum1 += ai
	print(sum1)

def main():
	m = int(input())
	while m:
		s = input().split(' ')
		(n, B, x, y) = (int(s[0]), int(s[1]), int(s[2]), int(s[3]))
		solution(n, B, x, y)
		m -= 1
main()


for _ in range(int(input())):
	(n, b, x, y) = map(int, input().split())
	ans = 0
	p = 0
	for i in range(n):
		if p + x > b:
			p -= y
		else:
			p += x
		ans += p
	print(ans)


x = int(input())
inp = list()
for i in range(x):
	l = list(map(int, input().split()))
	inp.append(l)
for i in range(len(inp)):
	n = inp[i][0]
	B = inp[i][1]
	x = inp[i][2]
	y = inp[i][3]
	temp = 0
	summ = 0
	for j in range(n):
		if temp + x > B:
			temp -= y
		else:
			temp += x
		summ += temp
	print(summ)


def main():
	tcs = int(input())
	for i in range(tcs):
		(n, B, x, y) = list(map(int, input().split()))
		mx = 0
		ap = 0
		for i in range(n):
			ax = ap + x
			ay = ap - y
			if ax > ay and ax <= B:
				mx += ax
				ap = ax
			elif ay > ax and ay <= B:
				mx += +ay
				ap = ay
			elif ax <= B and ay <= B:
				mx += max(ax, ay)
				ap = max(ax, ay)
			elif ax <= B:
				mx += ax
				ap = ax
			else:
				mx += ay
				ap = ay
		print(mx)
main()


def ans(n, B, x, y):
	a = [0]
	for i in range(n):
		if a[-1] + x > B:
			a.append(a[-1] - y)
		else:
			a.append(a[-1] + x)
	return sum(a)
t = int(input())
for i in range(t):
	(n, B, x, y) = map(int, input().split())
	print(ans(n, B, x, y))


a = int(input())
b = []
for i in range(a):
	(n, B, x, y) = map(int, input().split())
	q = 0
	u = 0
	if x < y and y <= 0:
		(x, y) = (y, x)
	for j in range(n):
		if u + x > B:
			q += u - y
			u -= y
		else:
			q += u + x
			u += x
	b.append(q)
for i in b:
	print(i)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for i in range(t):
	(n, B, x, y) = invr()
	(max_, prev) = (0, 0)
	for j in range(n):
		if prev + x <= B:
			prev += x
		else:
			prev -= y
		max_ += prev
	print(max_)


k = int(input())
res = [0 for i in range(k)]
for i in range(k):
	inp = list(map(int, input().split()))
	n = inp[0]
	B = inp[1]
	x = inp[2]
	y = inp[3]
	sum = 0
	a = 0
	if x > 0 and y > 0:
		for j in range(n):
			if a + x <= B:
				a += x
			else:
				a -= y
			sum += a
	else:
		print('zxc')
	res[i] = sum
for i in res:
	print(str(i))


import math
import sys
import statistics
from statistics import mode
input = sys.stdin.readline

def inp_int():
	return int(input())

def inp_list():
	return list(map(int, input().split()))

def inp_string():
	s = input()
	return list(s[:len(s) - 1])

def inp_reverse():
	return map(int, input().split())

def lst_to_str(lst):
	return ' '.join((str(e) for e in lst))

def is_square(i: int) -> bool:
	return i == int(math.sqrt(i)) ** 2

def solve():
	lst = inp_list()
	(n, B, x, y) = lst
	cnt = 0
	cur = 0
	for i in range(n):
		if cur + x <= B:
			cur += x
		else:
			cur -= y
		cnt += cur
	print(cnt)
number_cases = inp_int()
for x in range(number_cases):
	solve()


import math
for t in range(int(input())):
	(n, B, x, y) = map(int, input().split())
	sum = 0
	a = 0
	for i in range(n):
		if a + x <= B:
			a = a + x
		else:
			a = a - y
		sum += a
	print(sum)


def solve():
	(n, b, x, y) = [int(x) for x in input().split()]
	ans = 0
	prev = 0
	for i in range(n):
		if prev + x > b:
			prev = prev - y
		else:
			prev = prev + x
		ans += prev
	print(ans)
for tc in range(int(input())):
	solve()


for _ in range(int(input())):
	(n, b, x, y) = map(int, input().split())
	arr = [0]
	for _ in range(n):
		if arr[-1] + x <= b:
			arr.append(arr[-1] + x)
		else:
			arr.append(arr[-1] - y)
	print(sum(arr))


import math

def intlist():
	return [int(i) for i in input().split(' ')]

def stringlist():
	return input().split(' ')

def yes(t):
	if t:
		return 'YES'
	return 'NO'

def printjoin(l, str=False):
	if str:
		l = [str(i) for i in l]
	return ' '.join(l)

def power_of_two(n):
	b = bin(n)[2:]
	return int(b) == 10 ** (len(b) - 1)

def bit_count(n):
	return bin(n).count('1')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	lcm = x * y // gcd(x, y)
	return lcm

def isint(str):
	try:
		int(str)
		return True
	except ValueError:
		return False

def solve():
	(n, b, x, y) = intlist()
	s = 0
	last = 0
	for i in range(n):
		if last + x <= b:
			last += x
			s += last
		else:
			last -= y
			s += last
	return s
t = input()
for tt in range(int(t)):
	print(solve())


def solve():
	(N, B, X, Y) = map(int, input().split())
	sum1 = 0
	prev = 0
	for i in range(N):
		if prev + X > B:
			sum1 += prev - Y
			prev = prev - Y
		else:
			sum1 += prev + X
			prev = prev + X
	return sum1
remained_test_cases = 1
remained_test_cases = int(input())
while remained_test_cases > 0:
	print(solve())
	remained_test_cases -= 1


def solve():
	ans = [0]
	for i in range(1, n + 1):
		if ans[i - 1] + x <= B:
			ans.append(ans[i - 1] + x)
		else:
			ans.append(ans[i - 1] - y)
	return sum(ans)
from sys import stdin
input = stdin.readline
t = int(input())
for i in range(t):
	(n, B, x, y) = [int(x) for x in input().split()]
	print(solve())


for _ in range(int(input())):
	(n, b, x, y) = map(int, input().split())
	su = 0
	a = 0
	for i in range(1, n + 1):
		if a + x <= b:
			a = a + x
			su += a
		else:
			a = a - y
			su += a
	print(su)


t = int(input())
for i in range(t):
	(n, b, x, y) = map(int, input().split())
	a = 0
	s = 0
	if b >= n * x:
		s = int(n * (n + 1) * x * 0.5)
	else:
		for j in range(n):
			if a + x <= b:
				a = a + x
				s = s + a
			elif a + x > b:
				a = a - y
				s = s + a
	print(s)


t = int(input())
for i in range(0, t):
	(n, b, x, y) = list(map(int, input().split()))
	c = []
	c.append(0)
	k = 0
	for ele in range(0, n):
		if k + x > b:
			k = k - y
			c.append(k)
		else:
			k = k + x
			c.append(k)
	print(sum(c))


n = int(input())
for i in range(n):
	(m, s, x, y) = map(int, input().split())
	if x < -y:
		(x, y) = (y, x)
	last = 0
	summ = 0
	for i in range(m):
		if last + x <= s:
			last += x
		else:
			last -= y
		summ += last
	print(summ)


t = int(input())
while t:
	(n, b, x, y) = map(int, input().split())
	arr = [0]
	for i in range(1, n + 1):
		if arr[-1] + x <= b:
			arr.append(arr[-1] + x)
		else:
			arr.append(arr[-1] - y)
	print(sum(arr))
	t -= 1


t = int(input())
for _ in range(t):
	(n, b, x, y) = map(int, input().split())
	mx = cnt = 0
	for i in range(n):
		if cnt + x <= b:
			cnt += x
			mx += cnt
		else:
			cnt -= y
			mx += cnt
	print(mx)


n_test_cases = int(input())
results = []
for _ in range(n_test_cases):
	(n, B, x, y) = tuple([int(x) for x in input().split()])
	sum_ = 0
	vals = [0]
	for _ in range(n):
		if sum_ + x > B:
			sum_ -= y
		else:
			sum_ += x
		vals.append(sum_)
	results.append(sum(vals))
for result in results:
	print(result)


def calculateSum(n, b, x, y):
	prev = 0
	s = 0
	i = 0
	while i < n:
		if prev + x <= b:
			s += prev + x
			prev += x
		else:
			s += prev - y
			prev -= y
		i += 1
	return s
t = int(input())
for _ in range(t):
	(n, b, x, y) = map(int, input().split())
	print(calculateSum(n, b, x, y))


from cmath import sqrt
t = int(input())
while t:
	t -= 1
	(n, b, x, y) = map(int, input().split())
	sum = 0
	count = 0
	for i in range(n):
		if sum + x <= b:
			sum += x
		else:
			sum -= y
		count += sum
	print(count)


def solve(n, B, x, y):
	a = []
	sum = 0
	a.append(0)
	for i in range(n):
		v = a[i]
		if v + x > B:
			v = v - y
			a.append(v)
		else:
			v = v + x
			a.append(v)
	for i in a:
		sum += i
	return sum
t = int(input())
for i in range(t):
	(n, B, x, y) = map(int, input().split())
	print(solve(n, B, x, y))


import math
case = int(input())
for i in range(case):
	nums = input().split(' ')
	n = int(nums[0])
	B = int(nums[1])
	x = int(nums[2])
	y = int(nums[3])
	total = 0
	current = 0
	for j in range(n):
		if current + x <= B:
			current += x
			total += current
		else:
			current -= y
			total += current
	print(total)


for t in range(int(input())):
	l = input().split()
	(n, b, x, y) = (int(l[0]), int(l[1]), int(l[2]), int(l[3]))
	prev = 0
	accum = 0
	for i in range(1, n + 1):
		if prev + x > b:
			prev -= y
		else:
			prev += x
		accum += prev
	print(accum)


import sys

def main():
	t = int(sys.stdin.readline().strip())
	for _ in range(t):
		(n, b, x, y) = map(int, sys.stdin.readline().split())
		arr = [0] * (n + 1)
		for i in range(1, n + 1):
			if arr[i - 1] + x <= b:
				arr[i] = arr[i - 1] + x
			else:
				arr[i] = arr[i - 1] - y
		print(sum(arr))
main()


import math
for _ in range(int(input())):
	(n, B, x, y) = map(int, input().split())
	(total, a) = (0, 0)
	for i in range(n):
		if a + x <= B:
			a += x
		else:
			a -= y
		total += a
	print(total)


trails = int(input())
answer_list = []
for x in range(trails):
	numbers = input().split(' ')
	n = int(numbers[0])
	b = int(numbers[1])
	x = int(numbers[2])
	y = int(numbers[3])
	ans = 0
	cur = 0
	for g in range(n):
		if cur + x <= b:
			cur += x
		else:
			cur -= y
		ans += cur
	answer_list.append(ans)
for g in answer_list:
	print(g)


def func(n, b, x, y):
	curr = 0
	s = 0
	for i in range(n):
		if curr + x <= b:
			curr += x
		else:
			curr -= y
		s += curr
	return s
t = int(input())
for _ in range(t):
	(n, b, x, y) = list(map(int, input().split()))
	print(func(n, b, x, y))


tests = int(input())
for i in range(tests):
	(n, B, x, y) = list(map(int, input().split()))
	ans = 0
	ar = 0
	for i in range(n):
		if ar + x <= B:
			ar += x
		else:
			ar -= y
		ans += ar
	print(ans)


t = int(input())
for _ in range(t):
	(n, b, x, y) = map(int, input().split())
	res = 0
	pre = 0
	while n > 0:
		if pre + x <= b:
			pre = pre + x
		else:
			pre = pre - y
		res += pre
		n -= 1
	print(res)


for _ in range(int(input())):
	(n, b, x, y) = input().split()
	n = int(n)
	b = int(b)
	x = int(x)
	y = int(y)
	sum1 = 0
	runi = 0
	for i in range(0, n):
		if runi + x <= b:
			runi = runi + x
			sum1 = sum1 + runi
		elif runi + x > b:
			runi = runi - y
			sum1 = sum1 + runi
	print(sum1)


for _ in range(int(input())):
	(n, B, x, y) = [int(i) for i in input().split()]
	(num, summation) = (0, 0)
	for i in range(n):
		if num + x <= B:
			num += x
		else:
			num -= y
		summation += num
	print(summation)


t = input()
t = int(t)
S = []
for k in range(t):
	S.append(0)
for j in range(t):
	line = input()
	line = line.split()
	n = int(line[0])
	B = int(line[1])
	x = int(line[2])
	y = int(line[3])
	S[j] = 0
	a = 0
	for i in range(n):
		if a + x <= B:
			a += x
		else:
			a -= y
		S[j] += a
for p in range(t):
	print(S[p])


import math
t = int(input())
for _ in range(t):
	(n, B, x, y) = map(int, input().split())
	ans = 0
	a = 0
	for _ in range(n):
		if a + x <= B:
			a = a + x
		else:
			a = a - y
		ans = ans + a
	print(ans)


t = int(input())
for i in range(t):
	(n, B, x, y) = list(map(int, input().split()))
	ans = 0
	z = 0
	for i in range(n):
		if z + x <= B:
			z = z + x
			ans += z
		else:
			z = z - y
			ans += z
	print(ans)


j = int(input())
for i in range(j):
	(n, b, x, y) = input().split()
	n = int(n)
	b = int(b)
	x = int(x)
	y = int(y)
	st = 0
	su = 0
	for i in range(n):
		if st + x > b:
			st -= y
		else:
			st += x
		su += st
	print(su)


for _ in range(int(input())):
	(neijfeijdeid, beeyeyyehsdhoa, xuehuwehd, yasdhsuaddh) = list(map(int, input().split()))
	alskdjsufbuvf = 0
	zsdakdlalaaaa = 0
	for njksdakjsdkaj in range(neijfeijdeid):
		if zsdakdlalaaaa + xuehuwehd <= beeyeyyehsdhoa:
			zsdakdlalaaaa = zsdakdlalaaaa + xuehuwehd
			alskdjsufbuvf += zsdakdlalaaaa
		else:
			zsdakdlalaaaa = zsdakdlalaaaa - yasdhsuaddh
			alskdjsufbuvf += zsdakdlalaaaa
	print(alskdjsufbuvf)


for _ in range(int(input())):
	(n, b, x, y) = map(int, input().split())
	result = 0
	previous = 0
	while n > 0:
		if previous + x <= b:
			previous += x
		else:
			previous -= y
		result += previous
		n -= 1
	print(result)


t = int(input())
for i in range(t):
	(n, B, x, y) = map(int, input().split())
	sumi = 0
	ai = 0
	for i in range(n):
		if ai + x <= B:
			ai = ai + x
			sumi += ai
		else:
			ai = ai - y
			sumi += ai
	print(sumi)


def ans(li):
	k = 0
	sum = 0
	while li[0] != 0:
		k = k + li[2]
		if k > li[1]:
			k = k - li[2] - li[3]
		sum = sum + k
		li[0] = li[0] - 1
	print(sum)
t = int(input())
while t != 0:
	li = [int(i) for i in input().split()]
	ans(li)
	t = t - 1


import sys
import math
T = int(input())
for _ in range(T):
	(n, B, x, y) = [int(i) for i in input().split()]
	sm = 0
	ans = 0
	for i in range(n):
		if sm + x > B:
			sm -= y
		else:
			sm += x
		ans += sm
	print(ans)


for _ in range(int(input())):
	(n, B, x, y) = list(map(int, input().split(' ')))
	N = 0
	S = 0
	for i in range(n):
		if N > B or N + x > B:
			N -= y
			S += N
		else:
			N += x
			S += N
	print(S)


t = int(input())
for tt in range(0, t):
	(n, b, x, y) = map(int, input().split())
	last = 0
	sum = 0
	for i in range(1, n + 1):
		if last + x > b:
			last -= y
			sum += last
		else:
			last += x
			sum += last
	print(sum)


t = int(input())
for _ in range(t):
	(n, b, x, y) = map(int, input().split())
	a = [0]
	for i in range(n):
		if a[i] + x <= b:
			a.append(a[i] + x)
		else:
			a.append(a[i] - y)
	print(sum(a))


n = int(input())
for i in range(n):
	(n, B, x, y) = map(int, input().split())
	cnt = 0
	tin = 0
	for j in range(n):
		temp = tin + x
		if temp <= B:
			cnt = cnt + temp
			tin = temp
		else:
			temp = tin - y
			cnt = cnt + temp
			tin = temp
	print(cnt)


t = int(input())

def solve(n, b, x, y):
	ans = [0]
	c = 0
	while c < n:
		prev = ans[-1]
		if x + prev <= b:
			ans.append(x + prev)
		else:
			ans.append(prev - y)
		c += 1
	return sum(ans)
for _ in range(t):
	(n, b, x, y) = map(int, input().split())
	print(solve(n, b, x, y))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
import heapq
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 998244353

def func():
	(n, b, x, y) = mul()
	sm = 0
	st = 0
	idx = 0
	while idx < n:
		if st + x > b:
			st -= y
			sm += st
			idx += 1
		else:
			st += x
			sm += st
			idx += 1
	print(sm)
t = inp()
for i in range(t):
	func()


def solve(t):
	(n, B, x, y) = list(map(int, input().split(' ')))
	res = 0
	sum = 0
	for i in range(n):
		if res + x > B:
			res -= y
		else:
			res += x
		sum += res
	print(sum)
for t in range(int(input())):
	solve(t)


def solve():
	temp = input().split()
	(n, B, x, y) = (int(temp[0]), int(temp[1]), int(temp[2]), int(temp[3]))
	res = 0
	arr = [0] * (n + 1)
	for i in range(1, n + 1):
		if arr[i - 1] + x <= B:
			arr[i] = arr[i - 1] + x
		else:
			arr[i] = arr[i - 1] - y
	for x in arr:
		res += x
	print(res)
for case in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	(n, b, x, y) = map(int, input().split())
	sy = 0
	e = 0
	for i in range(n):
		if e + x <= b:
			e += x
		else:
			e -= y
		sy += e
	print(sy)


for _ in range(int(input())):
	(a, b, x, y) = map(int, input().split())
	sums = 0
	s = 0
	for i in range(a):
		if s + x <= b:
			s = s + x
			sums += s
		else:
			s -= y
			sums += s
	print(sums)


from math import *
import sys

def distance(x, w):
	d = x * x + w * w
	return d
for _ in range(int(input())):
	(n, b, x, y) = map(int, input().split())
	r = f = 0
	for i in range(n):
		if r + x > b:
			r -= y
			f += r
		else:
			r += x
			f += r
	print(f)


n = int(input())
for _ in range(n):
	(n, b, x, y) = [int(i) for i in input().split()]
	l = [0]
	for i in range(0, n):
		if l[i] + x <= b:
			l.append(l[i] + x)
		else:
			l.append(l[i] - y)
	print(sum(l))


import math
for i in range(int(input())):
	(n, B, x, y) = map(int, input().split())
	a = 0
	s = 0
	for k in range(n):
		if a + x <= B:
			s += a + x
			a = a + x
		else:
			s += a - y
			a = a - y
	print(s)


def solve(n, b, x, y):
	maxPos = 0
	prev = 0
	for i in range(1, n + 1):
		if prev + x > b:
			maxPos += prev - y
			prev = prev - y
		else:
			maxPos += prev + x
			prev = prev + x
	return maxPos
T = int(input())
for _ in range(T):
	(n, b, x, y) = list(map(int, input().split(' ')))
	print(solve(n, b, x, y))


m = int(input())
j = 0
while j < m:
	j += 1
	(n, b, x, y) = input().split()
	(n, b, x, y) = (int(n), int(b), int(x), int(y))
	p = 0
	sum = 0
	for i in range(n):
		if p + x <= b:
			p += x
			sum += p
		else:
			p -= y
			sum += p
	print(sum)


t = int(input())
for i in range(t):
	(n, b, x, y) = map(int, input().split())
	(ans, curr) = (0, 0)
	for j in range(n):
		curr += x if curr + x <= b else -y
		ans += curr
	print(ans)


test_cases = int(input())
for _ in range(test_cases):
	(n, b, x, y) = list(map(int, input().split()))
	sequence_array = []
	a = 0
	for i in range(n):
		if a + x <= b:
			a += x
			sequence_array.append(a)
		else:
			a -= y
			sequence_array.append(a)
	print(sum(sequence_array))