There is crazy man named P29892P. He always tries to do crazy things as he thinks. One day he invented a machine and named it as ANGEN. The ANGEN is used to perform range operations. The range operation means performing operations on range values from {L..R}. At any time it performs operations on values in between L to R.
ANGEN can perform following operations
U I V - Update the value present at I  with value V
A L R - Find the sum between range L and R
M L R - Find the maximum number between L and R
m L R - Find the minimum number between L and R
S L R - Find second maximum value in between L and R
s L R - Find second mimimum value in between L and R
If it is not possible perform operation ANGEN returns “NA” with out quotes.
For Invalid operations ANGEN returns “!!!” with out quotes.
Now P29892P challenging his friends and you too, to build his invention with yourown code. So it's your time to defeat P29892P by implementing his invention with your own ability. Let's go and solve the problem.

-----Input-----
Input description.
- The first line of the input contains an integer N denoting the number of integers. 
- The next line contains N space separated integers."
- The next line contains a single integer Q denoting the number of Questions.
-  The next Q lines contains T Question type , L and R.

-----Output-----
Print output for each question in separate line.

-----Constraints-----
Should contain all the constraints on the input data that you may have. Format it like:
- 1 ≤ N ≤ 100000
- 1 ≤ values[i] ≤ 1000000000
- 1 ≤ Q ≤ 10000
- T in { A..Z, a..z }
- 1 ≤ L ≤ R ≤ N

-----Example-----
Input:
6
1 2 5 3 10 6
6
A 1 5
M 1 3
m 5 6
s 3 6
U 1 7
S 1 2

Output:
21
5
6
5
2

-----Explanation-----
...

Difficulty: UNKNOWN_DIFFICULTY

Solutions:
VQ = 'UAMmSs'
n = int(input())
a = list(map(int, input().split()))
for _ in range(int(input())):
	(q, x, y) = input().split()
	if q not in VQ:
		print('!!!')
		continue
	if q == 'U':
		a[int(x) - 1] = int(y)
		continue
	l = int(x) - 1
	r = int(y)
	if q == 'A':
		print(sum(a[l:r]))
		continue
	if q == 'M':
		print(max(a[l:r]))
		continue
	if q == 'm':
		print(min(a[l:r]))
		continue
	s = sorted(set(a[l:r]))
	if len(s) < 2:
		print('NA')
	else:
		print(s[1] if q == 's' else s[-2])


n = eval(input())
arr = list(map(int, input().split()))
q = eval(input())
while q:
	q -= 1
	ar = input().split()
	t = ar[0]
	l = int(ar[1])
	r = int(ar[2])
	l -= 1
	if t == 'U':
		arr[l] = r
	elif t == 'A':
		print(sum(arr[l:r]))
	elif t == 'M':
		print(max(arr[l:r]))
	elif t == 'm':
		print(min(arr[l:r]))
	elif t == 'S':
		m = max(arr[l:r])
		m2 = -1
		for i in range(l, r):
			if arr[i] < m and arr[i] > m2:
				m2 = arr[i]
		print(m2)
	elif t == 's':
		m = min(arr[l:r])
		m2 = 1000000000000
		for i in range(l, r):
			if arr[i] > m and arr[i] < m2:
				m2 = arr[i]
		print(m2)


n = eval(input())
a = list(map(int, input().split()))
q = eval(input())
while q:
	s = input().split()
	if s[0] == 'U':
		l = int(s[1])
		v = int(s[2])
		a[l - 1] = v
	if s[0] == 'A':
		l = int(s[1])
		r = int(s[2])
		print(sum(a[l - 1:r]))
	if s[0] == 'M':
		l = int(s[1])
		r = int(s[2])
		print(max(a[l - 1:r]))
	if s[0] == 'm':
		l = int(s[1])
		r = int(s[2])
		print(min(a[l - 1:r]))
	if s[0] == 'S':
		l = int(s[1])
		r = int(s[2])
		A = sorted(a[l - 1:r], reverse=True)
		print(A[1])
	if s[0] == 's':
		l = int(s[1])
		r = int(s[2])
		A = sorted(a[l - 1:r])
		print(A[1])
	q -= 1


n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
for i in range(0, q):
	t = input().split()
	L = int(t[1]) - 1
	R = int(t[2]) - 1
	if L < 0 or L >= len(a):
		print('!!!')
	elif t[0] == 'U':
		a[L] = R
	elif R < 0 or R >= len(a) or L > R:
		print('!!!')
	elif t[0] == 'A':
		print(sum(a[L:R + 1]))
	elif t[0] == 'M':
		x = list(set(a[L:R + 1]))
		print(max(x))
	elif t[0] == 'm':
		x = list(set(a[L:R + 1]))
		x.remove(max(x))
		try:
			print(max(x))
		except:
			print('NA')
	elif t[0] == 'S':
		x = list(set(a[L:R + 1]))
		print(min(x))
	elif t[0] == 's':
		x = list(set(a[L:R + 1]))
		x.remove(min(x))
		try:
			print(min(x))
		except:
			print('NA')
	else:
		print('!!!')


def func(t, l, r):
	if t == 'U':
		try:
			a[l - 1] = r
		except:
			print('NA')
	elif t == 'A':
		try:
			print(sum(a[l - 1:r]))
		except:
			print('NA')
	elif t == 'M':
		try:
			print(max(a[l - 1:r]))
		except:
			print('NA')
	elif t == 'm':
		try:
			print(min(a[l - 1:r]))
		except:
			print('NA')
	elif t == 'S':
		try:
			bb = list(set(a[l - 1:r]))
			bb.sort()
			print(bb[-2])
		except:
			print('NA')
	elif t == 's':
		try:
			bb = list(set(a[l - 1:r]))
			bb.sort()
			print(bb[1])
		except:
			print('NA')
	else:
		print('!!!')
n = eval(input())
a = list(map(int, input().split()))
q = eval(input())
for i in range(q):
	(t, l, r) = input().split()
	try:
		func(t, int(l), int(r))
	except:
		print('!!!')


def na(l, r):
	if l - 1 >= r or l > n or r > n:
		return True
	else:
		return False

def na_print():
	print('NA')

def func(t, l, r):
	if t == 'U':
		a[l - 1] = r
		return ''
	elif na(l, r):
		na_print()
		return ''
	if t == 'A':
		print(sum(a[l - 1:r]))
	elif t == 'M':
		print(max(a[l - 1:r]))
	elif t == 'm':
		print(min(a[l - 1:r]))
	elif t == 'S':
		bb = list(set(a[l - 1:r]))
		bb.sort()
		print(bb[-2])
	elif t == 's':
		bb = list(set(a[l - 1:r]))
		bb.sort()
		print(bb[1])
	else:
		print('!!!')
n = eval(input())
a = list(map(int, input().split()))
q = eval(input())
for i in range(q):
	(t, l, r) = input().split()
	func(t, int(l), int(r))