Dima's got a staircase that consists of n stairs. The first stair is at height a_1, the second one is at a_2, the last one is at a_{n} (1 ≤ a_1 ≤ a_2 ≤ ... ≤ a_{n}). 

Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width w_{i} and height h_{i}. Dima throws each box vertically down on the first w_{i} stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., w_{i}. Each thrown box flies vertically down until at least one of the two following events happen:  the bottom of the box touches the top of a stair;  the bottom of the box touches the top of a box, thrown earlier. 

We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width w_{i} cannot touch the stair number w_{i} + 1.

You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.


-----Input-----

The first line contains integer n (1 ≤ n ≤ 10^5) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9; a_{i} ≤ a_{i} + 1).

The next line contains integer m (1 ≤ m ≤ 10^5) — the number of boxes. Each of the following m lines contains a pair of integers w_{i}, h_{i} (1 ≤ w_{i} ≤ n; 1 ≤ h_{i} ≤ 10^9) — the size of the i-th thrown box.

The numbers in the lines are separated by spaces.


-----Output-----

Print m integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.


-----Examples-----
Input
5
1 2 3 6 6
4
1 1
3 1
1 1
4 3

Output
1
3
4
6

Input
3
1 2 3
2
1 1
3 1

Output
1
3

Input
1
1
5
1 2
1 10
1 10
1 10
1 10

Output
1
3
13
23
33



-----Note-----

The first sample are shown on the picture. [Image]

Difficulty: MEDIUM

Solutions:
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = {}
t = []
for i in range(m):
	(w, h) = list(map(int, input().split()))
	x = max(a[0], a[w - 1])
	t.append(x)
	a[0] = x + h
	a[w - 1] = x + h
for i in range(m):
	print(t[i])


m = int(input())
arr = list(map(int, input().split()))
t = int(input())
for i in range(t):
	(w, h) = map(int, input().split())
	p = max(arr[0], arr[w - 1])
	print(p)
	arr[0] = p + h


import sys
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline())
x = 0
for i in range(m):
	(w, h) = map(int, sys.stdin.readline().split())
	x = max(x, l[w - 1])
	sys.stdout.write(str(x) + '\n')
	x += h


from sys import stdin

def ip():
	return stdin.readline().split()
n = int(stdin.readline())
li = list(map(int, ip()))
m = int(stdin.readline())
for _ in range(m):
	(w, h) = map(int, ip())
	print(max(li[0], li[w - 1]))
	li[0] = max(li[0], li[w - 1]) + h


n = int(input())
l = list(map(int, input().split()))
m = int(input())
s = []
for i in range(m):
	s += [list(map(int, input().split()))]
h = []
a = 0
for i in range(m):
	b = l[s[i][0] - 1]
	t = max(b, a + 1)
	h += [t]
	a = t + s[i][1] - 1
for i in h:
	print(i)


n = int(input())
import sys
l = [0] + list(map(int, sys.stdin.readline().strip().split()))
x = 0
b = 0
m = int(input())
for _ in range(m):
	(w, h) = map(int, sys.stdin.readline().strip().split())
	x = max(x + b, l[w])
	b = h
	print(x)


n = input()
stairs = list(map(int, input().split(' ')))
boxes = []
for i in range(int(input())):
	boxes.append(list(map(int, input().split(' '))))
ch = stairs[0]
ans = []
for box in boxes:
	nh = max(ch, stairs[box[0] - 1])
	ans.append(nh)
	ch = nh + box[1]
print(*ans, sep='\n')


import os
import sys
from io import BytesIO, IOBase
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 gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def seive(n):
	primes = [True] * (n + 1)
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	return primes

def factors(n):
	factors = []
	x = 2
	while x * x <= n:
		while n % x == 0:
			factors.append(x)
			n //= x
		if n > 1:
			factors.append(x)
	return factors

def main():
	try:
		n = inp()
		a = inlt()
		m = inp()
		ans = 0
		for i in range(m):
			(w, h) = invr()
			value = a[w - 1]
			ans = max(value, ans)
			print(ans)
			ans += h
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
ans = [0]
l = 0
h_prev = 0
for i in range(1, m + 1):
	(w, h) = map(int, input().split())
	x = max(ans[i - 1] + h_prev, a[w - 1])
	ans.append(x)
	h_prev = h
for i in range(1, m + 1):
	print(ans[i])


from sys import stdin
input = stdin.readline
n = int(input())
s = [0] + [int(item) for item in input().split(' ')]
pre = s[0]
q = int(input())
x = {}
lowest = s[1]
while q:
	[w, h] = [int(item) for item in input().split(' ')]
	height = max(lowest, s[w])
	lowest = height + h
	print(height)
	q -= 1


_ = input()
a = list(map(int, input().split(' ')))
m = input()
res = list()
for _ in range(int(m)):
	(w, h) = map(int, input().split(' '))
	cmax = max(a[0], a[w - 1])
	res.append(cmax)
	a[0] = cmax + h
print('\n'.join(map(str, res)))


def main():
	n = int(input())
	hts = list(map(int, input().split()))
	ans = []
	max_ht = 0
	m = int(input())
	for i in range(m):
		(w, h) = map(int, input().split())
		curr_ht = hts[w - 1]
		if max_ht == 0:
			ans.append(curr_ht)
			max_ht = curr_ht + h
		elif curr_ht >= max_ht:
			max_ht = curr_ht + h
			ans.append(curr_ht)
		else:
			ans.append(max_ht)
			max_ht += h
	for i in range(m):
		print(ans[i])
main()


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
import heapq
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'
mod = 10 ** 9 + 7

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def ispali(s):
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	n = ii()
	arr = li()
	last = 0
	m = ii()
	ans = ''
	for i in range(m):
		(w, h) = mi()
		x = max(arr[w - 1], last)
		ans += str(x) + '\n'
		last = max(x + h, last + h)
	print(ans)
main()


from io import BytesIO, IOBase
import sys, os
inpl = lambda : list(map(int, input().split()))
inpm = lambda : map(int, input().split())
inpi = lambda : int(input())
inp = lambda : input()
(rev, ra, l) = (reversed, range, len)
P = print
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
n = inpi()
li = inpl()
m = inpi()
ans = 0
for i in ra(m):
	(w, h) = inpm()
	ans = max(ans, li[w - 1])
	P(ans)
	ans += h


n = int(input())
a = list(map(int, input().split()))
m = int(input())
w = []
h = []
for i in range(m):
	(x, y) = map(int, input().split())
	w.append(x)
	h.append(y)
cur = 0
for i in range(m):
	cur = max(cur, a[w[i] - 1])
	print(cur)
	cur += h[i]


n = int(input())
A = [0] + [int(x) for x in input().split()]
m = int(input())
curHeight = 0
Answer = []
for i in range(m):
	(a, b) = map(int, input().split())
	curHeight = max(curHeight, A[a])
	Answer.append(str(curHeight))
	curHeight += b
print('\n'.join(Answer))


def main():
	(n, res) = (int(input()), [])
	l = list(map(int, input().split()))
	for _ in range(int(input())):
		(w, h) = map(int, input().split())
		m = max(l[0], l[w - 1])
		res.append(m)
		l[0] = m + h
	print('\n'.join(map(str, res)))
main()


import sys

def main():
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split(' ')))
	m = int(sys.stdin.readline())
	(w1, h1) = (0, 0)
	H = 0
	for i in range(m):
		(w2, h2) = map(int, sys.stdin.readline().split(' '))
		H = max(H + h1, a[w2 - 1])
		sys.stdout.write('{}\n'.format(str(H)))
		(w1, h1) = (w2, h2)
main()


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = []
for _ in range(m):
	b.append(list(map(int, input().split())))
for (x, y) in b:
	m = max(a[0], a[x - 1])
	print(m)
	a[0] = m + y


import sys
input = sys.stdin.readline
(n, staircase) = (int(input()), [int(i) for i in input().split()])
(boxes, best) = ([[int(i) for i in input().split()] for _ in range(int(input()))], 0)
for box in boxes:
	best = max(staircase[box[0] - 1], best)
	print(best)
	best += box[1]


n = int(input())
a = list(map(int, input().split()))
m = int(input())
l = [list(map(int, input().split())) for _ in range(m)]
ans = [a[l[0][0] - 1]]
lasth = l[0][1]
for (_, [w, h]) in enumerate(l[1:]):
	ans.append(max(ans[-1] + lasth, a[w - 1]))
	lasth = h
print(*ans, sep='\n')


n = int(input())
arr = list(map(int, input().split()))
(idx, res) = (0, [])
m = int(input())
for _ in range(m):
	(w, h) = map(int, input().split())
	r = max(arr[w - 1], idx)
	res.append(r)
	arr[w - 1] = r + h
	idx = max(idx, arr[w - 1])
print('\n'.join(map(str, res)))


n = int(input())
lis = list(map(int, input().split()))
a = int(input())
que = []
hi = []
co = 0
for i in range(a):
	que.append(list(map(int, input().split())))
i = 0
while i < a:
	(w, h) = que[i]
	if lis[w - 1] >= w:
		if lis[w - 1] >= co:
			hi.append(lis[w - 1])
			co = max(co, lis[w - 1] + h)
		else:
			hi.append(co)
			co += h
	elif w >= co:
		hi.append(lis[w - 1])
		co = max(co, lis[w - 1] + h)
	else:
		hi.append(co)
		co += h
	i += 1
for i in hi:
	print(i)


def check(n, a, m, dimensions):
	result = list()
	prev = 0
	for ele in dimensions:
		w = ele[0]
		h = ele[1]
		if w <= prev:
			maxHeight = a[prev]
		else:
			maxHeight = max(a[prev:w])
			prev = w - 1
		result.append(maxHeight)
		a[prev] = maxHeight + h
	return result
n = int(input().rstrip())
a = list(map(int, input().rstrip().split()))
m = int(input().rstrip())
dimensions = list()
for i in range(m):
	temp = list(map(int, input().rstrip().split()))
	w = temp[0]
	h = temp[1]
	dimensions.append((w, h))
result = check(n, a, m, dimensions)
for i in result:
	print(i)


__author__ = 'runekri3'
stairs_amount = int(input())
stair_heights = list(map(int, input().split()))
boxes_amount = int(input())
boxes = []
for _ in range(boxes_amount):
	boxes.append(list(map(int, input().split())))
for (width, height) in boxes:
	box_bottom = max(stair_heights[0], stair_heights[width - 1])
	print(box_bottom)
	stair_heights[0] = box_bottom + height


n = int(input())
a = list(map(int, input().split()))
for i in range(int(input())):
	(w, h) = map(int, input().split())
	k = max(a[0], a[w - 1])
	a[0] = k + h
	print(k)


import math
from collections import defaultdict as dt
from sys import stdin
inp = lambda : stdin.readline().strip()
intinp = lambda : int(inp())
mod = int(1000000000.0) + 7
inf = float('inf')
listinp = lambda : list(map(int, stdin.readline().split()))
mapinp = lambda : map(int, stdin.readline().split())
for _ in range(1):
	n = intinp()
	a = listinp()
	m = intinp()
	for i in range(m):
		(w, h) = mapinp()
		l = max(a[0], a[w - 1])
		print(l)
		a[0] = l + h


n = int(input())
l = list(map(int, input().split()))
g = l[0]
m = int(input())
S = ''
for i in range(m):
	(a, b) = list(map(int, input().split()))
	t = max(l[0], l[a - 1])
	S += str(t) + '\n'
	l[0] = t + b
	l[a - 1] = t + b
print(S)


__autor__ = 'Esfandiar'
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
M = -float('inf')
for i in range(m):
	(w, h) = list(map(int, input().split()))
	if a[w - 1] < M:
		print(M)
		M += h
	else:
		print(a[w - 1])
		a[w - 1] += h
		M = a[w - 1]


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def find_parent(parent, i, cnt):
	cnt += 1
	if parent[i - 1] == i:
		return cnt
	if parent[i - 1] != i:
		find_parent(parent, parent[i - 1], cnt)
from collections import Counter
from collections import Counter
import math
n = int(input())
arr = list(map(int, input().split()))
m = int(input())
box = []
for i in range(m):
	(u, v) = map(int, input().split())
	box.append((u, v))
ans = 0
val = 0
for i in range(m):
	ans = max(val + ans, arr[box[i][0] - 1])
	val = box[i][1]
	print(ans)


from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import sys
import math
import heapq
import string
import random
(MIN, MAX, MOD) = (-1061109567, 1061109567, 1000000007)

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
n = N()
a = RLL()
m = N()
hit = 0
for _ in range(m):
	(w, h) = RL()
	res = max(a[w - 1], hit)
	print(res)
	hit = max(hit, res + h)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
(m,) = I()
k = l[0]
for i in range(m):
	(x, y) = I()
	k = max(k, l[min(n - 1, x - 1)])
	print(k)
	k += y


from sys import stdin
inp = lambda : list(map(int, stdin.readline().split()))
inp()
aa = inp()
for i in range(inp()[0]):
	(a, b) = inp()
	k = max(aa[0], aa[a - 1])
	aa[0] = k + b
	print(k)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
box = []
for i in range(m):
	box.append(list(map(int, input().split())))
(l, w1, h1) = (0, 0, 0)
ans = []
for i in range(m):
	l = max(l + h1, a[box[i][0] - 1])
	ans.append(l)
	w1 = box[i][0]
	h1 = box[i][1]
for i in ans:
	print(i)


n = int(input())
arr = [int(i) for i in input().split()]
arr.insert(0, 0)
m = int(input())
last = 1
for _ in range(m):
	(a, b) = map(int, input().split())
	if a >= last and arr[a] > arr[last]:
		print(arr[a])
		arr[a] += b
		last = a
	else:
		print(arr[last])
		arr[last] += b


n = int(input())
l = list(map(int, input().split()))
m = int(input())
for i in range(m):
	(w, h) = map(int, input().split())
	ans = max(l[0], l[w - 1])
	print(ans)
	l[0] = ans + h


stair_number = int(input())
stair_heights = list(map(int, input().split()))
box_number = int(input())
x = stair_heights[0]
for _ in range(box_number):
	(w, h) = input().split()
	(w, h) = (int(w), int(h))
	if x >= stair_heights[w - 1]:
		k = x
	else:
		k = stair_heights[w - 1]
	print(k)
	x = k + h