Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Yesterday, puppy Tuzik learned a magically efficient method to find the sum of the integers from 1 to N. He denotes it as sum(N). But today, as a true explorer, he defined his own new function: sum(D, N), which means the operation sum applied D times: the first time to N, and each subsequent time to the result of the previous operation.

For example, if D = 2 and N = 3, then sum(2, 3) equals to sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 21.

Tuzik wants to calculate some values of the sum(D, N) function. Will you help him with that?

------ Input ------ 

The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing two integers D and N.

------ Output ------ 

For each testcase, output one integer on a separate line.

------ Constraints ------ 

$1 ≤ T ≤ 16$
$1 ≤ D, N ≤ 4$

----- Sample Input 1 ------ 
2

1 4

2 3
----- Sample Output 1 ------ 
10

21

Source: codechef | Difficulty: EASY
Tags: Mathematics

Implementation


Solutions:
for i in range(int(input())):
	(a, b) = [int(i) for i in input().split()]
	sum = 0
	p = 0
	for i in range(a):
		sum = 0
		for j in range(1, b + 1):
			sum += j
		b = sum
	print(sum)


t = int(input())
for i in range(t):
	(d, n) = map(int, input().split())
	s = 1
	for i in range(1, d + 1):
		s = n * (n + 1) // 2
		n = s
	print(s)


T = int(input())
for o in range(T):
	(D, N) = map(int, input().split())
	S = 1
	for i in range(1, D + 1):
		S = N * (N + 1) // 2
		N = S
	print(S)


for _ in range(int(input())):
	(d, n) = map(int, input().split())
	s = 0
	while d:
		s = n * (n + 1) // 2
		d -= 1
		n = s
	print(s)


n = int(input())
for i in range(n):
	(p, q) = map(int, input().split())
	s = 0
	for j in range(p):
		s = sum(list(range(1, q + 1)))
		q = s
	print(s)


n = int(input())

def sumdata(num):
	return sum([i for i in range(num + 1)])
for i in range(n):
	(n1, n2) = map(int, input().split())
	for j in range(n1):
		n2 = sumdata(n2)
	print(n2)


def SUM(num):
	total = 0
	for i in range(num + 1):
		total += i
	return total
for t in range(int(input())):
	(loops, number) = map(int, input().split())
	while loops:
		number = SUM(number)
		loops -= 1
	print(number)


def sum(d, n):
	if d == 1:
		return n * (n + 1) // 2
	else:
		n = n * (n + 1) // 2
		return sum(d - 1, n)
for i in range(int(input())):
	(d, n) = map(int, input().split())
	print(sum(d, n))


def sum(b):
	m = 0
	for i in range(1, b + 1):
		m = m + i
	return m
n = int(input())
for i in range(n):
	x = input()
	x = list(map(int, x.split()))
	for j in range(x[0]):
		x[1] = sum(x[1])
	print(x[1])


for _ in range(int(input())):
	(d, n) = map(int, input().split())
	for j in range(d):
		l = []
		res = 0
		for i in range(1, n + 1):
			l.append(i)
		res = res + sum(l)
		n = res
	print(res)


def add(a):
	s = 0
	for j in range(1, a + 1):
		s += j
	return s
t = int(input())
for i in range(t):
	(d, n) = map(int, input().split())
	for k in range(d):
		n = add(n)
	print(n)


T = int(input())
for i in range(T):
	s = 0
	(d, n) = map(int, input().split())
	for j in range(1, n + 1):
		s += j
	c = s
	for j in range(d - 1):
		z = 0
		for k in range(1, c + 1):
			z += k
		c = z
	print(c)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	for i in range(a):
		s = b * (b + 1) // 2
		b = s
	print(s)


T = int(input())
for _ in range(T):
	(a, b) = map(int, input().split())
	for i in range(0, a):
		b = b * (b + 1) // 2
	print(b)


t = int(input())
for i in range(t):

	def sums(d, n):
		for i in range(d):
			n = n * (n + 1) // 2
		return n
	(a, b) = map(int, input().split())
	result = sums(a, b)
	print(result)


t = int(input())
for i in range(t):
	(d, n) = map(int, input().split())
	c = 0
	while d != 0:
		c = n * (n + 1) // 2
		n = c
		d -= 1
	print(c)


def sum1(n):
	return n * (n + 1) // 2
n = int(input())
for i in range(n):
	(d, a) = list(map(int, input().split()))
	mm = d
	for j in range(mm):
		m = sum1(a)
		a = m
	print(m)


def sum(n):
	sum = 0
	for i in range(1, n + 1):
		sum += i
	return sum
for i in range(int(input())):
	x = input().split(' ')
	a = int(x[0])
	b = int(x[1])
	answer = 0
	for j in range(a):
		answer = sum(b)
		b = answer
	print(answer)


for i in range(int(input())):
	(p, n) = map(int, input().split())
	S = 1
	for i in range(1, p + 1):
		S = n * (n + 1) // 2
		n = S
	print(S)


T = int(input())
for i in range(T):
	(D, N) = map(int, input().split())
	sum = 0
	for i in range(D):
		sum = N * (N + 1) // 2
		N = sum
	print(sum)


def sum(n):
	for i in range(d):
		a = n * (n + 1) / 2
		n = a
	return print(int(a))
for i in range(int(input())):
	(d, n) = map(int, input().split())
	sum(n)


for i in range(int(input())):
	(d, n) = map(int, input().split())
	sum = 0
	for j in range(d):
		x = n * (n + 1) // 2
		n = x
	print(n)


a = int(input())
li = []
for i in range(a):
	b = list(map(int, input().split()))
	li.append(b)
for j in range(len(li)):
	for m in range(li[j][0]):
		sum = 0
		for k in range(li[j][1] + 1):
			sum = sum + k
		li[j][1] = sum
	print(sum)


t = int(input())
for i in range(t):
	(d, n) = map(int, input().split())
	while d > 0:
		sum1 = 0
		for i in range(1, n + 1):
			sum1 = sum1 + i
		d = d - 1
		n = sum1
	print(sum1)


t = int(input())
for i in range(t):
	(D, N) = map(int, input().split())
	a = 1
	for i in range(1, D + 1):
		a = N * (N + 1) // 2
		N = a
	print(a)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	n = 1
	for i in range(1, a + 1):
		n = b * (b + 1) // 2
		b = n
	print(n)


for _ in range(int(input())):
	(d, n) = map(int, input().split())
	for i in range(d):
		n = n * (n + 1) // 2
	print(n)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	count = 0
	sum = 0
	d = 0
	for i in range(a):
		for j in range(1, b + 1):
			sum += j
			b = sum - d
		d = sum
	count = b
	print(count)


d = int(input())
for i in range(d):
	(w, r) = map(int, input().split())
	for i in range(0, w):
		r = r * (r + 1) // 2
	print(r)


def sum(n):
	s = 0
	for i in range(1, n + 1):
		s = s + i
	return s
t = int(input())
for _ in range(t):
	(d, n) = map(int, input().split())
	s = sum(n)
	for i in range(1, d):
		s = sum(s)
	print(s)


n = int(input())
for i in range(n):
	(p, q) = map(int, input().split())
	s = 0
	for i in range(1, p + 1):
		s = q * (q + 1) // 2
		q = s
	print(s)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	sm = 0
	for j in range(a):
		sm = b * (b + 1) // 2
		b = sm
	print(sm)


for i in range(int(input())):
	(d, n) = map(int, input().split())
	for i in range(d):
		temp = 0
		for i in range(1, n + 1):
			temp += i
		n = temp
	print(n)


def sum(D, N):
	for i in range(D):
		N = N * (N + 1) // 2
	return N
T = int(input())
for i in range(T):
	(D, N) = map(int, input().split())
	result = sum(D, N)
	print(result)


def getSum(n):
	sum = 0
	for i in range(n, 0, -1):
		sum += i
	return sum

def sum(d, n):
	if d == 1:
		return getSum(n)
	else:
		return getSum(sum(d - 1, n))
for _ in range(int(input())):
	(d, n) = map(int, input().split())
	print(sum(d, n))


def sum_a(n):
	y = int(n * (n + 1) / 2)
	return y
for i in range(int(input())):
	(d, n) = map(int, input().split())
	sum = 0
	for j in range(d):
		a = sum_a(n)
		n = a
	print(n)


t = int(input())

def add(n):
	return sum(range(1, n + 1))

def Sum(d, n):
	r = 0
	while d > 0:
		r = add(n)
		n = r
		d -= 1
	return r
for _ in range(t):
	(d, n) = map(int, input().split())
	print(Sum(d, n))


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	for j in range(a):
		sum = b * (b + 1) // 2
		b = sum
	print(b)


T = int(input())
for i in range(T):
	(D, N) = map(int, input().split())
	s = N * (N + 1) / 2
	for i in range(D - 1):
		s = s * (s + 1) / 2
	print(int(s))


n = int(input())
for j in range(n):
	(a, b) = map(int, input().split())
	e = 1
	for i in range(1, a + 1):
		e = b * (b + 1) // 2
		b = e
	print(e)


num = int(input())
for i in range(num):
	(D, N) = map(int, input().split())
	S = 1
	for i in range(1, D + 1):
		S = N * (N + 1) // 2
		N = S
	print(S)


T = int(input())
for j in range(T):
	(D, N) = map(int, input().split())
	S = 1
	for j in range(1, D + 1):
		S = N * (N + 1) // 2
		N = S
	print(S)