def find_min_freq_integer(A, Q):
N = len(A)
freq = [0] * N
for _ in range(Q):
l, r = map(int, input().split())
l -= 1 # Adjust for 0-based indexing
r -= 1
min_freq = (r - l + 1) // 3 + 1
# Update frequency array for the range [l, r]
for i in range(l, r + 1):
freq[A[i] - 1] += 1
# Find the minimum integer with frequency > min_freq
min_integer = -1
for i in range(N):
if freq[i] >= min_freq and (min_integer == -1 or A[i] < A[min_integer]):
min_integer = i
print(A[min_integer] if min_integer != -1 else -1)
# Reset frequency array for the next query
for i in range(l, r + 1):
freq[A[i] - 1] = 0
# Read input
N, Q = map(int, input().split())
A = list(map(int, input().split()))
find_min_freq_integer(A, Q)