A driver is driving on the freeway. The check engine light of his vehicle is on, and the driver wants to get service immediately. Luckily, a service lane runs parallel to the highway. It varies in width along its length.

You will be given an array of widths at points along the road (indices), then a list of the indices of entry and exit points. Considering each entry and exit point pair, calculate the maximum size vehicle that can travel that segment of the service lane safely.

Example 

$n = 4$ 

$width = [2,3,2,1]$ 

$cases = [[1,2],[2,4]]$  

If the entry index, $i=1$ and the exit, $j=2$, there are two segment widths of $2$ and $3$ respectively.  The widest vehicle that can fit through both is $2$.  If $i=2$ and $j=4$, the widths are $[3,2,1]$ which limits vehicle width to $1$.  

Function Description  

Complete the serviceLane function in the editor below.  

serviceLane has the following parameter(s):  

int n: the size of the $uwidth$ array  
int cases[t][2]: each element contains the starting and ending indices for a segment to consider, inclusive    

Returns  

int[t]: the maximum width vehicle that can pass through each segment of the service lane described

Input Format

The first line of input contains two integers, $n$ and $t$, where $n$ denotes the number of width measurements and $t$, the number of test cases. The next line has $n$ space-separated integers which represent the array $width$.

The next $t$ lines contain two integers, $i$ and $j$, where $i$ is the start index and $j$ is the end index of the segment to check.

Constraints

$2\le n\le100000$  
$1\leq t\leq1000$  
$0\leq i<j<n$  
$2\leq j-i+1\leq min(n,1000)$  
$1\leq width[k]\leq3,$where $0\leq k<n$

Sample Input
STDIN               Function
-----               --------
8 5                 n = 8, t = 5
2 3 1 2 3 2 3 3     width = [2, 3, 1, 2, 3, 2, 3, 3]
0 3                 cases = [[0, 3], [4, 6], [6, 7], [3, 5], [0, 7]]
4 6
6 7
3 5
0 7

Sample Output
1
2
3
2
1

Explanation

Below is the representation of the lane:

   |HIGHWAY|Lane|    ->    Width

0: |       |--|            2
1: |       |---|           3
2: |       |-|             1
3: |       |--|            2
4: |       |---|           3
5: |       |--|            2
6: |       |---|           3
7: |       |---|           3

$(0,3)$: From index $0$ through $3$ we have widths $2,3,1$ and $2$.  Nothing wider than $1$ can pass all segments. 
$(4,6)$: From index $4$ through $6$ we have widht $3,2$ and $3$.  Nothing wider than $2$ can pass all segments.
$(6,7)$: $3,3 → 3$ .
$(3,5)\colon 2,3,2\to2$
$(0,7)\colon2,3,1,2,3,2,3,3,3\to1$.

Source: hackerrank | Difficulty: EASY
Tags: Implementation


Solutions:
import sys
(N, T) = [int(item) for item in next(sys.stdin).split()]
W = [int(item) for item in next(sys.stdin).split()]
for line in sys.stdin:
	(i, j) = [int(item) for item in line.split()]
	print(min(W[i:j + 1]))


import sys

def main():
	line = sys.stdin.readline()
	items = line.split()
	n = int(items[0])
	t = int(items[1])
	line = sys.stdin.readline()
	items = line.split()
	lane = list(map(int, items))
	for i in range(t):
		line = sys.stdin.readline()
		items = line.split()
		start = int(items[0])
		end = int(items[1])
		print(min(lane[start:end + 1]))
main()


arrData = []

def main():
	inp = input().split()
	n = int(inp[0])
	t = int(inp[1])
	global arrData
	arrData = list(map(int, input().split()))
	for i in range(t):
		arrIn = list(map(int, input().split()))
		result = getMin(arrIn[0], arrIn[1])
		print(result)

def getMin(start, end):
	num = 4
	global arrData
	for i in range(start, end + 1):
		if arrData[i] < num:
			num = arrData[i]
	return num
main()


def main():
	t = int(input().split()[1])
	list = [int(i) for i in input().split()]
	for i in range(t):
		rangelist = [int(i) for i in input().split()]
		print(min(list[rangelist[0]:rangelist[1] + 1]))
main()


inp = input()
N = int(inp.split()[0])
T = int(inp.split()[1])
arr_str = input().split()
arr = []
for item in arr_str:
	arr.append(int(item))
for i in range(T):
	inp = input()
	start = int(inp.split()[0])
	stop = int(inp.split()[1])
	m = 3
	for j in range(stop - start + 1):
		if arr[start + j] < m:
			m = arr[start + j]
	print(m)


import sys
(N, T) = [int(x) for x in sys.stdin.readline().split()]
widths = [int(x) for x in sys.stdin.readline().split()]

def find_max(start, end, widths):
	vehicle = 3
	for w in range(start, end + 1):
		if widths[w] < vehicle:
			vehicle = widths[w]
	return vehicle
for case in sys.stdin:
	(start, end) = [int(x) for x in case.split()]
	print(find_max(start, end, widths))


def service(entry, exit, lane):
	return min(lane[entry:exit + 1])
a = input().strip()
a = list(map(int, a.split(' ')))
t = a[1]
a = input().strip()
lane = list(map(int, a.split(' ')))
for i in range(t):
	b = input().strip()
	(entry, exit) = tuple(map(int, b.split(' ')))
	print(service(entry, exit, lane))


x = [int(i) for i in input().split()]
lane = [int(i) for i in input().split()]
for i in range(x[1]):
	points = [int(i) for i in input().split()]
	print(min(lane[points[0]:points[1] + 1]))


line1 = input()
line1 = list(map(int, line1.split()))
testCases = line1[1]
roadSize = line1[0]
road = input()
road = list(map(int, road.split()))
for i in range(0, testCases):
	line = input()
	line = list(map(int, line.split()))
	print(min(road[line[0]:line[1] + 1]))


(n, t) = (int(i) for i in input().split())
wid = [int(i) for i in input().split()]
for i in range(t):
	(a, b) = (int(i) for i in input().split())
	print(min(wid[a:b + 1]))


s = input().strip().split(' ')
n = int(s[0])
t = int(s[1])
w = map(int, input().strip().split(' '))
w = list(w)
while t > 0:
	s = input().split(' ')
	i = int(s[0])
	j = int(s[1])
	k = min((w[e] for e in range(i, j + 1)))
	print(k)
	t -= 1


(N, T) = [int(x) for x in input().strip().split(' ')]
L = [int(x) for x in input().strip().split(' ')]
for t in range(0, T):
	(i, j) = [int(x) for x in input().strip().split(' ')]
	print(min(L[i:j + 1]))


def g(st):
	st1 = st.split()
	l1 = []
	for i in range(0, len(st1)):
		l1.append(int(st1[i]))
	return l1

def h(l1, t):
	l = [0]
	for i in range(1, t + 1):
		ki = input()
		l.append(f(l1, g(ki)))
	for i in range(1, len(l)):
		print(l[i])

def f(l1, l2):
	l3 = l1[l2[0]:l2[1] + 1]
	d = min(l3)
	return d
st = input()
st1 = input()
h(g(st1), g(st)[1])


(N, T) = (int(x) for x in input().split())
width = list((int(x) for x in input().split()))
for _ in range(T):
	(i, j) = (int(x) for x in input().split())
	largest = 3
	for segment in range(i, j + 1):
		if width[segment] == 1:
			largest = 1
			break
		elif width[segment] < largest:
			largest = width[segment]
	print(largest)


(N, T) = (int(k) for k in input().split())
lane = [int(k) for k in input().split()]
for i in range(N):
	try:
		minWidth = 9
		(en, ex) = (int(k) for k in input().split())
		for j in range(en, ex + 1):
			if lane[j] < minWidth:
				minWidth = lane[j]
		print(minWidth)
	except EOFError:
		pass


(N, T) = [int(v) for v in input().split()]
freeway = [int(v) for v in input().split()]
for t in range(T):
	(start, stop) = [int(v) for v in input().split()]
	print(min(freeway[start:stop + 1]))


import fileinput
lst = []
for row in fileinput.input():
	lst.append(row.strip())

def pass_vehicles(inp):
	parseInput = [x.split(' ') for x in inp]
	lane = [int(width) for width in parseInput[1]]
	testCases = [[int(y) for y in x] for x in parseInput[2:]]
	testsOut = []
	for test in testCases:
		maximum = 3
		for x in range(test[0], test[1] + 1):
			if lane[x] < maximum:
				maximum = lane[x]
		testsOut.append(maximum)
	for nr in testsOut:
		print(nr)
pass_vehicles(lst)


import sys
parameter = sys.stdin.readline().split()
N = int(parameter[0])
T = int(parameter[1])
width_raw = sys.stdin.readline().split()
width = []
for index in range(N):
	width.append(int(width_raw[index]))
cases = []
for index in range(T):
	case = sys.stdin.readline().split()
	cases.append((int(case[0]), int(case[1])))
for case in cases:
	entry = case[0]
	exit = case[1]
	narrowest = 3
	for index in range(entry, exit + 1):
		if width[index] < narrowest:
			narrowest = width[index]
	print(narrowest)


(n, t) = [int(l) for l in input().strip().split()]
width = list(map(int, input().split()))
for k in range(0, t):
	(i, j) = [int(k) for k in input().strip().split()]
	veh_type = width[i]
	temp = i
	while temp <= j:
		if veh_type > width[temp]:
			veh_type = width[temp]
		temp += 1
	print(veh_type)


import sys
input = list(sys.stdin)
widths = [int(k) for k in input[1].split()]
for row in input[2:]:
	(start, end) = (int(k) for k in row.split())
	print(min(widths[start:end + 1]))


(N, T) = input().split()
T = int(T)
N = int(N)
width_input = input().split()
width = [int(w) for w in width_input]
for testcase in range(T):
	(i, j) = input().split()
	i = int(i)
	j = int(j)
	m = min(width[i:j + 1])
	print(m)


__author__ = 'Murugan'

def getMin(lst, ee):
	entry = ee[0]
	ext = ee[1]
	min = lst[entry]
	for i in range(entry, ext + 1):
		if min > lst[i]:
			min = lst[i]
	return min
buffer = input().split()
N = int(buffer[0])
T = int(buffer[1])
lane = input().split()
lane = list(map(int, lane))
for i in range(T):
	buffer = list(map(int, input().split()))
	print(getMin(lane, buffer))


(n, t) = map(int, input().split())
width = [int(x) for x in input().split()]
for _ in range(t):
	(i, j) = map(int, input().split())
	m = 3
	for k in range(i, j + 1):
		m = min(m, width[k])
	print(m)


s = input().strip().split()
n = int(s[0])
t = int(s[1])
s = input().strip().split()
w = [int(x) for x in s]
while t:
	t -= 1
	s = input().strip().split()
	i = int(s[0])
	j = int(s[1])
	print(min(w[i:j + 1]))


(n, t) = map(int, input().split())
ws = list(map(int, input().split()))
for ti in range(t):
	(i, j) = map(int, input().split())
	print(min(ws[i:j + 1]))


import sys
(nLen, nCases) = map(int, sys.stdin.readline().split())
w = tuple(map(int, sys.stdin.readline().split()))
for iCase in range(nCases):
	(iIn, iOut) = map(int, sys.stdin.readline().split())
	print(min(w[iIn:iOut + 1]))


n_and_t = input().split()
n = n_and_t[0]
t = n_and_t[1]
width = input().split()
routes = []
vehicles = []
for i in range(int(t)):
	routes.append(input())
for j in routes:
	entrance = int(j.split()[0])
	eexit = int(j.split()[1])
	minwidth = 3
	for k in range(entrance, eexit + 1):
		cur_width = int(width[k])
		if cur_width < minwidth:
			minwidth = cur_width
	vehicles.append(minwidth)
for vehicle in vehicles:
	print(vehicle)


inp = input().split()
t = int(inp[0])
n = int(inp[1])
lane = input().split()
for num in range(n):
	case = input().split()
	start = int(case[0])
	end = int(case[1])
	lowestval = 3
	for i in range(start, end + 1):
		if int(lane[i]) < lowestval:
			lowestval = int(lane[i])
	print(lowestval)


def minimum(l, start, final):
	_min = l[start]
	i = start + 1
	while True:
		if _min == 1:
			return _min
		else:
			if _min > l[i]:
				_min = l[i]
			i += 1
			if i > final:
				return _min

def main():
	(n, t) = [int(x) for x in input().split()]
	st = input()
	answers = []
	width = []
	width = [int(x) for x in st.split()]
	for i in range(t):
		(start, final) = [int(x) for x in input().split()]
		answers.append(minimum(width, start, final))
	for item in answers:
		print(item)
main()


(length, T) = [int(n) for n in input().split()]
road = [int(r) for r in input().split()]
for case in range(T):
	(entry, exit) = [int(s) for s in input().split()]
	print(min(road[entry:exit + 1]))


import sys

def main():
	(n, t) = (int(i) for i in sys.stdin.readline().split())
	highway = [int(i) for i in sys.stdin.readline().split()]
	for _ in range(t):
		(i, j) = (int(i) for i in sys.stdin.readline().split())
		print(min(highway[i:j + 1]))
main()


(n, t) = map(int, input().split())
arr = list(map(int, input().split()))
last_seen = []
prev_seen = [-1, -1]
for i in range(n):
	if arr[i] < 3:
		prev_seen[arr[i] - 1] = i
	last_seen.append(prev_seen[:])

def get_best(i, j):
	for w in (1, 2):
		ls = last_seen[j][w - 1]
		if ls >= i:
			return w
	return 3
for _ in range(t):
	(i, j) = map(int, input().split())
	print(get_best(i, j))


scan = lambda : map(int, input().split())
(n, t) = scan()
width = list(scan())
for i in range(t):
	(i, j) = scan()
	print(min(width[i:j + 1]))


import sys

def main():
	(N, T) = [int(n) for n in sys.stdin.readline().split(' ', 1)]
	width_list = [int(n) for n in sys.stdin.readline().split(' ', N - 1)]
	testcases_list = []
	for t in range(T):
		testcase_pair = [int(n) for n in sys.stdin.readline().split(' ', 1)]
		testcases_list.append(testcase_pair)
	for t in testcases_list:
		print(biggest_vehicle(t[0], t[1], width_list))

def biggest_vehicle(start, end, width_list):
	biggest_vehicle = width_list[start]
	for k in range(start, end + 1):
		if biggest_vehicle > width_list[k]:
			biggest_vehicle = width_list[k]
	return biggest_vehicle
main()


import sys
[N, T] = [int(X) for X in sys.stdin.readline().split()]
widths = [int(X) for X in sys.stdin.readline().split()]
no_car = {i for i in range(N) if widths[i] < 2}
car_but_no_truck = {i for i in range(N) if widths[i] < 3} - no_car
for case in range(T):
	[i, j] = [int(X) for X in sys.stdin.readline().split()]
	segment = set(range(i, j + 1))
	if segment & no_car:
		print(1)
	elif segment & car_but_no_truck:
		print(2)
	else:
		print(3)


import sys

def main():
	(n, t) = (int(i) for i in sys.stdin.readline().split())
	road = [int(i) for i in sys.stdin.readline().split()]
	for _ in range(t):
		(i, j) = (int(i) for i in sys.stdin.readline().split())
		print(min(road[i:j + 1]))
main()


def naiveImplementation(length, testcases, widths):
	for (start, end) in testcases:
		if start < 0 or end > length:
			print(0)
			continue
		print(min(widths[start:end + 1]))
finalImplementation = naiveImplementation

def inputParser():
	(N, T) = [int(x) for x in input().split()]
	W = [int(x) for x in input().split()]
	TCs = [[int(x) for x in input().split()] for _ in range(T)]
	return (N, TCs, W)
(inputLength, inputTestcases, inputWidths) = inputParser()
finalImplementation(inputLength, inputTestcases, inputWidths)


import sys
f = sys.stdin
size = [int(i) for i in f.readline().split()]
arr = [int(i) for i in f.readline().split()]
i = 0
while i < size[1]:
	val = [int(i) for i in f.readline().split()]
	sub = arr[val[0]:val[1] + 1]
	print(min(sub))
	i += 1


def MaxV(xs, ws):
	i = int(xs[0])
	max = 3
	while i <= int(xs[1]):
		if ws[i] < max:
			max = ws[i]
		else:
			i = i + 1
	return max
l = input().split()
n = int(l[0])
t = int(l[1])
w = list(map(int, input().split()))
for i in range(t):
	x = input().split()
	print(MaxV(x, w))


inp = int(input().split()[1])
list = [int(i) for i in input().split()]
for i in range(inp):
	rangelist = [int(i) for i in input().split()]
	print(min(list[rangelist[0]:rangelist[1] + 1]))


import sys
(n, t) = tuple([int(x) for x in sys.stdin.readline().split()])
width = [int(x) for x in sys.stdin.readline().split()]
tests = [tuple(x.split()) for x in sys.stdin.readlines()]
for (a, b) in tests:
	m = min(width[int(a):int(b) + 1])
	print(3 if m >= 3 else m)


(N, T) = input().split()
width = input().split()
for i in range(int(T)):
	inputs = input().split()
	print(min(width[int(inputs[0]):int(inputs[1]) + 1]))


import sys
sys.stdin.readline()
widths = [int(i) for i in sys.stdin.readline().split()]
for testcase in sys.stdin:
	start = int(testcase.split()[0])
	end = int(testcase.split()[1])
	print(min(widths[start:end + 1]))


import sys
(N, T) = list(map(int, sys.stdin.readline().split()))
width = list(map(int, sys.stdin.readline().split()))
for _ in range(T):
	(i, j) = list(map(int, sys.stdin.readline().split()))
	print(min(width[i:j + 1]))


line = input()
line = line.split()
N = int(line[0])
T = int(line[1])
line1 = input()
line1 = line1.split()
for i in range(T):
	line2 = input()
	line2 = line2.split()
	a = int(line2[0])
	b = int(line2[1])
	size = min(line1[a:b + 1])
	print(size)


import sys

def processWidth(widthList, N):
	twos = [-1] * N
	ones = [-1] * N
	processingTwos = []
	processingThrees = []
	for i in range(N):
		if int(widthList[i]) == 1:
			for index in processingTwos:
				ones[index] = i
			for index in processingThrees:
				ones[index] = i
			processingTwos = []
			processingThrees = []
		if int(widthList[i]) == 2:
			for index in processingThrees:
				if twos[index] == -1:
					twos[index] = i
					processingTwos.append(index)
			processingThrees = []
			processingTwos.append(i)
		if int(widthList[i]) == 3:
			processingThrees.append(i)
	return (ones, twos)
lines = []
for line in sys.stdin:
	lines.append(line)
params = lines[0][:-1].split()
N = int(params[0])
T = int(params[1])
data = lines[1][:-1].split()
tests = lines[2:]
for i in range(T - 1):
	tests[i] = tests[i][:-1]
(ones, twos) = processWidth(data, N)
for test in tests:
	inNout = test.split()
	i = int(inNout[0])
	j = int(inNout[1])
	result = int(data[i])
	if result == 3:
		if j >= twos[i] and twos[i] > 0:
			result = 2
		if j >= ones[i] and ones[i] > 0:
			result = 1
	if result == 2:
		if j >= ones[i] and ones[i] > 0:
			result = 1
	print(result)


import sys
line = sys.stdin.readline().strip().split(' ')
n = int(line[0])
t = int(line[1])
line = sys.stdin.readline().strip().split(' ')
widths = [int(i) for i in line]
for test_case in range(0, t):
	line = sys.stdin.readline().strip().split(' ')
	(i, j) = (int(line[0]), int(line[1]))
	answer = widths[i]
	for k in range(i, j + 1):
		answer = min(answer, widths[k])
	print(answer)


(N, T) = input().split()
N = int(N)
T = int(T)
arr = input().split()
for case in range(T):
	(begin, end) = input().split()
	max = 3
	for i in range(int(begin), int(end) + 1):
		if int(arr[i]) < max:
			max = int(arr[i])
	print(max)


from sys import stdin
(n, t) = list(map(int, stdin.readline().split()))
l = list(map(int, stdin.readline().split()))
for i in range(t):
	(a, b) = list(map(int, stdin.readline().split()))
	print(min(l[a:b + 1]))


Len_Test = input().split()
w = input().split()
for i in range(int(Len_Test[1])):
	S = input().split()
	z = w[int(S[0]):int(S[1]) + 1]
	c = min(z)
	print(int(c))


(N, T) = input().split()
width_list = input().split()
width_list = [int(width_list[i]) for i in range(int(N))]
for t in range(int(T)):
	(i, j) = input().split()
	r = width_list[int(i):int(j)] + [width_list[int(j)]]
	print(min(r))


(N, T) = map(int, input().split())
widths = list(map(int, input().split()))
cases = [map(int, input().split()) for i in range(T)]
for (enter, exit) in cases:
	min_width = 3
	for width in widths[enter:exit + 1]:
		if width < min_width:
			min_width = width
		if min_width == 1:
			break
	print(min_width)


(N, t) = [int(x) for x in input().split()]
width = [int(x) for x in input().split()]

def solve(i, j):
	res = width[j]
	for x in width[i:j]:
		if x < res:
			res = x
	print(res)
for test in range(t):
	(i, j) = [int(x) for x in input().split()]
	solve(i, j)


(N, T) = [int(x) for x in input().split()]
widths = [int(x) for x in input().split()]
for test in range(T):
	(i, j) = [int(x) for x in input().split()]
	print(min(widths[i:j + 1]))


t = int(str(input()).split()[1])
sl = [int(val) for val in str(input()).split()]
for i in range(t):
	(s, e) = [int(val) for val in str(input()).split()]
	min = 3
	for i in range(s, e + 1):
		if sl[s] < min:
			min = sl[s]
		s += 1
	print(min)


(n, t) = str(input()).split()
n = int(n)
t = int(t)
sl = [int(val) for val in str(input()).split()]
for i in range(t):
	(a, b) = str(input()).split()
	a = int(a)
	b = int(b)
	mini = 3
	while a <= b:
		if sl[a] < mini:
			mini = sl[a]
		a += 1
	print(mini)


import sys
data = sys.stdin.read()
data = data.split()
for i in range(len(data)):
	data[i] = int(data[i])
N = data.pop(0)
T = data.pop(0)
width = []
for i in range(N):
	width.append(data.pop(0))
cases = list(zip(data[0::2], data[1::2]))
soln = []
for n in range(len(cases)):
	soln.append(min(width[cases[n][0]:cases[n][1] + 1]))
for i in soln:
	print(i)


from sys import stdin
(N, T) = [int(i) for i in stdin.readline().split()]
width = [int(i) for i in stdin.readline().split()]
for t in range(T):
	(i, j) = [int(i) for i in stdin.readline().split()]
	min_width = 3
	for w in width[i:j + 1]:
		if w < min_width:
			min_width = w
			if min_width == 1:
				break
	print(min_width)


line = input()
nt = [int(i) for i in line.split()]
line = input()
width = [int(i) for i in line.split()]
for _ in range(nt[1]):
	line = input()
	inout = [int(i) for i in line.split()]
	print(min(width[inout[0]:inout[1] + 1]))


(N, T) = str(input()).split()
N = int(N)
T = int(T)
a = str(input()).split()
for i in range(T):
	(I, J) = str(input()).split()
	res = a[int(I)]
	for j in range(int(I) + 1, int(J) + 1):
		if res > a[j]:
			res = a[j]
	print(res)


import sys
data = sys.stdin.readlines()
length = int(data[0].split()[0])
cases = int(data[0].split()[1])
widths = [int(s) for s in data[1].split()]
segments = []
for i in range(2, 2 + cases):
	item = [int(s) for s in data[i].split()]
	segments.append((item[0], item[1]))
for s in segments:
	segment = widths[s[0]:s[1] + 1]
	segment.sort()
	print(segment[0])


inp = input().split()
(N, T) = (int(inp[0]), int(inp[1]))
width = input().split()
for i in range(T):
	inp2 = input().split()
	maxWidth = 3
	for j in range(int(inp2[0]), int(inp2[1]) + 1):
		curWidth = int(width[j])
		if curWidth < maxWidth:
			maxWidth = curWidth
	print(maxWidth)


(n, t) = [int(i) for i in input().strip().split()]
w = [int(i) for i in input().strip().split()]
for test in range(0, t):
	(i, j) = [int(i) for i in input().strip().split()]
	path = w[i:j + 1]
	v = min(path)
	print(v)


n = 0
tc = 0
(n, tc) = map(int, input().split())
vw = [0] * n
vw = list(map(int, input().split()))
for c in range(tc):
	(s, f) = map(int, input().split())
	sm = vw[s]
	while s <= f and s < n:
		if sm > vw[s]:
			sm = vw[s]
		s = s + 1
	print(sm)


(N, T) = [int(x) for x in input().split()]
widths = [int(x) for x in input().split()]
for t in range(T):
	(i, j) = [int(x) for x in input().split()]
	print(min(widths[i:j + 1]))


the_string = input()
lane = []
entry = []
exit_seg = []
list = []
(freeway, test_case) = the_string.split()
min1 = int(freeway)
lane_string = input()
for each in lane_string.split():
	lane.append(each)
for _ in range(int(test_case)):
	segment_string = input()
	(start, end) = segment_string.split()
	entry.append(start)
	exit_seg.append(end)
for each in range(int(test_case)):
	min1 = int(freeway)
	for x in range(int(entry[each]), int(exit_seg[each]) + 1):
		if min1 > int(lane[x]):
			min1 = int(lane[x])
	print(min1)


import sys
import math
[nSeg, t] = [int(i) for i in sys.stdin.readline().split()]
laneWidths = [int(i) for i in sys.stdin.readline().split()]
for i in range(t):
	[start, end] = [int(i) for i in sys.stdin.readline().split()]
	print(min(laneWidths[start:end + 1]))


import sys
line = sys.stdin.readline()
(n, t) = line.split()
(n, t) = (int(n), int(t))
width = [int(w) for w in sys.stdin.readline().split()]
line = sys.stdin.readline()
while line:
	(i, j) = line.split()
	(i, j) = (int(i), int(j))
	m = 3
	while i <= j:
		if m > width[i]:
			m = width[i]
			if m == 1:
				break
		i += 1
	print(m)
	line = sys.stdin.readline()


numCases = int(input().split()[1])
highway = [int(x) for x in input().split()]
for x in range(0, numCases):
	testCase = [int(x) for x in input().split()]
	print(min(highway[testCase[0]:testCase[1] + 1]))


from sys import stdin, stdout
(N, T) = map(int, stdin.readline().split())
widths = list(map(int, stdin.readline().split()))
assert len(widths) == N
for _ in range(T):
	(i, j) = map(int, stdin.readline().split())
	print(min(widths[i:j + 1]))


import sys
(N, T) = input().split()
H = input().split()
for t in range(int(T)):
	(si, sj) = input().split()
	i = int(si)
	j = int(sj)
	mx = min(H[i:j + 1])
	print(mx)


(freeway_len, tests) = str(input()).split()
freeway_len = int(freeway_len)
tests = int(tests)
freeway_input = str(input()).split()
freeway_w = []
for width in freeway_input:
	freeway_w.append(int(width))
for test in range(tests):
	(i, j) = str(input()).split()
	i = int(i)
	j = int(j)
	print(min(freeway_w[i:j + 1]))


import sys
inputLines = [[int(y) for y in x.split()] for x in sys.stdin.readlines()]
(roadLength, testCases) = (inputLines[0][0], inputLines[0][1])
roadWidths = inputLines[1]
for test_case in inputLines[2:]:
	start = test_case[0]
	end = test_case[1]
	minWidth = 3
	for i in range(start, end + 1):
		if roadWidths[i] < minWidth:
			minWidth = roadWidths[i]
	print(minWidth)


T = input().split()
array = input().split()
newarray = []
for i in range(int(T[1])):
	k = input().split()
	for i in range(int(k[0]), int(k[1]) + 1):
		newarray.append(array[i])
	print(min(newarray))
	newarray = []


T = input().split()
n = int(T[0])
T = int(T[1])
N = []
i = input().split()
for c in range(n):
	N.append(int(i[c]))
for c in range(T):
	i = input().split()
	start = int(i[0])
	end = int(i[1]) + 1
	print(min(N[start:end]))