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

Given an array of size n, you have to answer queries of the form :  L R k . For each query, you have to find an element which occurs consecutively in the subarray [L,R] more than or equal to k times. k will always be greater than floor((R-L+1)/2). If no such element exists, print -1.

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

First line of the input contains 2 space separated N and M, denoting the size of the array and the number of queries. 
The next line contains N space separated integers, where the i-th integer A_{i} denotes the ith element of the array.
Each of the next M lines contains 3 integers space separated integers L, R and k.

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

Output M lines where each line contains the answer to the i-th query.

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

$1 ≤ N, M ≤ 10^{5}$
$1 ≤ L ≤ R ≤ N$
$floor((R-L+1)/2) < k ≤ R-L+1$    
$0 ≤ A_{i} ≤ 10^{9}$

------ Subtasks ------ 

Subtask #1 (10 points):
$1 ≤ A_{i} ≤ 3$ 
$1 ≤ M ≤ 10$    

Subtask #2 (30 points):
$1 ≤ N ≤ 1000$

Subtask #3 (60 points):
$No other constraints$

----- Sample Input 1 ------ 
5 1
1 2 2 2 2
1 5 3
----- Sample Output 1 ------ 
2
----- explanation 1 ------ 
The element 2 occurs more than 3 times in the range [1,5].

Difficulty: HARD

Solutions:
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
assert len(a) == n
lb = n * [None]
rb = n * [None]
i = 0
j = 0
while i < n:
	while j < n - 1 and a[j + 1] == a[j]:
		j += 1
	for k in range(i, j + 1):
		lb[k] = i
		rb[k] = j
	i = j + 1
	j = i
for _ in range(m):
	(l, r, k) = [int(x) for x in input().split()]
	l = l - 1
	r = r - 1
	c = (l + r) // 2
	ll = max(lb[c], l)
	rr = min(rb[c], r)
	print(a[c] if rr - ll + 1 >= k else -1)


(N, M) = list(map(int, input().split()))
A = list(map(int, input().split()))
z = 0
i = 0
dp = []
while i < N:
	dp.append([i, i])
	if i == N - 1:
		break
	while i < N - 1 and A[i] == A[i + 1]:
		dp.append(dp[-1])
		i += 1
		dp[-1][1] += 1
	i += 1
for i in range(M):
	(L, R, k) = list(map(int, input().split()))
	R -= 1
	L -= 1
	z = dp[(L + R) // 2]
	if min(R, z[1]) - max(L, z[0]) + 1 >= k:
		print(A[(L + R) // 2])
	else:
		print(-1)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(pre, cur) = ([], 0)
for i in range(len(a)):
	if i == 0 or a[i] == a[i - 1]:
		cur += 1
	else:
		cur = 1
	pre.append(cur)
for _ in range(m):
	(l, r, k) = map(int, input().split())
	l -= 1
	r -= 1
	(i, j) = (l + k - 1, r - k + 1)
	if a[i] == a[l] and pre[i] >= k:
		print(a[i])
		continue
	if a[j] == a[r] and pre[r] >= k:
		print(a[j])
		continue
	mid = (l + r) // 2
	if pre[mid] > mid - l + 1:
		d = pre[mid] - pre[l] + 1
	else:
		d = pre[mid]
	_j = mid + k - d
	if _j <= r and pre[_j] >= k:
		print(a[mid])
		continue
	print(-1)


from sys import stdin
input = stdin.readline

class segtree:

	def __init__(self, n):
		self.st = [0] * (4 * n)
		self.size = n - 1

	def update(self, x, value, l=0, h=-1, i=0):
		if h == -1:
			h = self.size
		if l > h or l > x or h < x:
			return
		if l == h:
			if l == x:
				self.st[i] = value
			return
		mid = (l + h) // 2
		self.update(x, value, l, mid, 2 * i + 1)
		self.update(x, value, mid + 1, h, 2 * i + 2)
		self.st[i] = max(self.st[2 * i + 1], self.st[2 * i + 2])

	def query(self, x, y, l=0, h=-1, i=0):
		if h == -1:
			h = self.size
		if l > y or h < x:
			return 0
		if l >= x and h <= y:
			return self.st[i]
		if l == h:
			return 0
		mid = (l + h) // 2
		left = self.query(x, y, l, mid, 2 * i + 1)
		right = self.query(x, y, mid + 1, h, 2 * i + 2)
		return max(left, right)

def b_s(a, key1, key2):
	(l, h) = (0, len(a) - 1)
	while l <= h:
		mid = (l + h) // 2
		if a[mid] < key1:
			l = mid + 1
			continue
		if a[mid] > key2:
			h = mid - 1
			continue
		return a[mid]

def answer():
	(st, pos) = (segtree(n), [[] for i in range(n + 1)])
	j = 0
	for i in range(n):
		while j < n and a[j] == a[i]:
			j += 1
		st.update(i, j - i)
		pos[j - i].append(i)
	for q in range(m):
		(l, r, k) = map(int, input().split())
		this = st.query(l - 1, r - k)
		if this >= k:
			print(a[b_s(pos[this], l - 1, r - k)])
		else:
			print(-1)
for T in range(1):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	answer()


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
left = [0] * n
right = [0] * n
left[0] = 0
for i in range(1, n):
	if arr[i] == arr[i - 1]:
		left[i] = left[i - 1]
	else:
		left[i] = i
right[n - 1] = n - 1
for i in range(n - 2, -1, -1):
	if arr[i] == arr[i + 1]:
		right[i] = right[i + 1]
	else:
		right[i] = i
for i in range(m):
	(L, R, K) = map(int, input().split())
	x = int((L + R) / 2 - 1)
	st = min(R - 1, right[x]) - max(L - 1, left[x]) + 1
	if st >= K:
		print(arr[left[x]])
	else:
		print(-1)


(n, q) = map(int, input().split())
l = [int(i) for i in input().split()]
left_arr = [0] * n
left_arr[0] = 0
for i in range(1, n):
	if l[i] == l[i - 1]:
		left_arr[i] = left_arr[i - 1]
	else:
		left_arr[i] = i
right_arr = [0] * n
right_arr[n - 1] = n - 1
for i in range(n - 2, -1, -1):
	if l[i] == l[i + 1]:
		right_arr[i] = right_arr[i + 1]
	else:
		right_arr[i] = i
for i in range(q):
	(left, right, k) = map(int, input().split())
	left -= 1
	right -= 1

	def is_k_consec(mid):
		peeche = max(left, left_arr[mid])
		aage = min(right, right_arr[mid])
		return aage - peeche + 1 >= k
	if is_k_consec((left + right) // 2):
		print(l[(right + left) // 2])
	else:
		print(-1)


from bisect import *
(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
if n == 1:
	for q in range(m):
		print(a[0])
clusters = []
current = a[0]
count = 1
for (i, v) in enumerate(a[1:]):
	if v == current:
		count += 1
		continue
	clusters.append([i - count + 2, count, current])
	current = v
	count = 1
clusters.append([i - count + 3, count, current])
cstarts = [c[0] for c in clusters]
for q in range(m):
	(l, r, k) = [int(i) for i in input().split()]
	if l == r:
		print(a[l - 1])
		continue
	c = clusters[bisect(cstarts, (l + r) / 2) - 1]
	x = max(l, c[0])
	y = min(r, c[0] + c[1] - 1)
	if y - x + 1 >= k:
		print(c[2])
	else:
		print(-1)


(sizeArr, noQuerie) = map(int, input().split())
Arr = list(map(int, input().split()))
left = [0] * sizeArr
right = [0] * sizeArr
left[0] = 0
for i in range(1, sizeArr):
	if Arr[i] == Arr[i - 1]:
		left[i] = left[i - 1]
	else:
		left[i] = i
right[sizeArr - 1] = sizeArr - 1
for i in range(sizeArr - 2, -1, -1):
	if Arr[i] == Arr[i + 1]:
		right[i] = right[i + 1]
	else:
		right[i] = i
for i in range(noQuerie):
	(L, R, K) = map(int, input().split())
	x = int((L + R) / 2 - 1)
	st = min(R - 1, right[x]) - max(L - 1, left[x]) + 1
	if st >= K:
		print(Arr[left[x]])
	else:
		print(-1)


import sys
s = input().split()
n = int(s[0])
m = int(s[1])
a = [int(x) for x in input().split()]
end = [0 for x in range(n)]
start = [0 for x in range(n)]
end[n - 1] = n - 1
for i in range(n - 2, -1, -1):
	if a[i] == a[i + 1]:
		end[i] = end[i + 1]
	else:
		end[i] = i
start[0] = 0
for i in range(1, n):
	if a[i] == a[i - 1]:
		start[i] = start[i - 1]
	else:
		start[i] = i
while m > 0:
	s = input().split()
	L = int(s[0])
	R = int(s[1])
	K = int(s[2])
	x = int((L + R) / 2 - 1)
	size = min(R - 1, end[x]) - max(L - 1, start[x]) + 1
	if size >= K:
		sys.stdout.write(str(a[x]) + '\n')
	else:
		sys.stdout.write('-1\n')
	m -= 1


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
size = len(arr)
left = [0] * len(arr)
right = [len(arr) - 1] * len(arr)
for i in range(0, size - 1):
	if arr[i] == arr[i + 1]:
		left[i + 1] = left[i]
	else:
		left[i + 1] = i + 1
for i in range(size - 2, -1, -1):
	if arr[i] == arr[i + 1]:
		right[i] = right[i + 1]
	else:
		right[i] = i
while m:
	m -= 1
	(l, r, k) = map(int, input().split())
	mid = int(l - 1 + (r - l) / 2)
	if min(right[mid], r - 1) - max(left[mid], l - 1) + 1 >= k:
		print(arr[mid])
	else:
		print(-1)


import math
(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
left = [0 for i in range(0, n)]
right = [0 for i in range(0, n)]
right[n - 1] = n - 1
for i in range(1, n):
	if a[i] == a[i - 1]:
		left[i] = left[i - 1]
	else:
		left[i] = i
for i in range(n - 2, -1, -1):
	if a[i] == a[i + 1]:
		right[i] = right[i + 1]
	else:
		right[i] = i
for i in range(0, m):
	(l, r, k) = map(int, input().split())
	l = l - 1
	r = r - 1
	p1 = r - l + 1
	p = math.floor(p1 / 2)
	p = p + l
	freq = min(right[p], r) - max(left[p], l) + 1
	if freq >= k:
		print(a[p])
	else:
		print('-1')


[n, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [0]
for i in range(1, n):
	if a[i] == a[i - 1]:
		b.append(b[-1])
	else:
		b.append(i)
c = [n - 1]
for i in range(n - 1)[::-1]:
	if a[i] == a[i + 1]:
		c.append(c[-1])
	else:
		c.append(i)
c = c[::-1]
for _ in range(m):
	[l, r, k] = [int(i) for i in input().split()]
	l -= 1
	r -= 1
	t = (l + r) // 2
	x = max(b[t], l)
	y = min(c[t], r)
	if x <= y and y - x + 1 >= k:
		print(a[t])
	else:
		print('-1')


(n, m) = map(int, input().split())
lst = list(map(int, input().split()))
lst = [0] + lst
prev = list(range(len(lst)))
suf = list(range(len(lst)))
for i in range(2, len(lst)):
	if lst[i - 1] == lst[i]:
		prev[i] = prev[i - 1]
for i in reversed(range(1, len(lst) - 1)):
	if lst[i + 1] == lst[i]:
		suf[i] = suf[i + 1]
for i in range(m):
	(l, r, k) = map(int, input().split())
	for mid in [(l + r) // 2, (l + r) // 2 + 1, 0]:
		ll = max(l, prev[mid])
		rr = min(r, suf[mid])
		if rr - ll + 1 >= k:
			print(lst[mid])
			break
		elif mid == 0:
			print(-1)


import sys

def main():
	(N, M) = list(map(int, sys.stdin.readline().split()))
	arr = list(map(int, sys.stdin.readline().split()))
	interv = [0 for _ in range(N)]
	prev = arr[0]
	start = 0
	for i in range(1, N):
		if arr[i] != prev:
			for j in range(start, i):
				interv[j] = (start, i - 1)
			prev = arr[i]
			start = i
	for j in range(start, N):
		interv[j] = (start, N - 1)
	for _ in range(M):
		(L, R, k) = list(map(int, sys.stdin.readline().split()))
		L -= 1
		R -= 1
		mid = (L + R) // 2
		(start, end) = interv[mid]
		start = max(start, L)
		end = min(end, R)
		if end - start + 1 >= k:
			print(arr[mid])
		else:
			print(-1)
main()


(n, t) = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
b = [1] * n
temp = a[0]
for i in range(1, n):
	if a[i] == temp:
		b[i] = b[i - 1] + 1
	temp = a[i]
for i in range(t):
	(L, R, k) = list(map(int, input().strip().split()))
	c = b[L + k - 2]
	if c >= k:
		print(a[L - 1])
		continue
	else:
		d = L + 2 * k - c - 2
		if d <= R - 1 and b[d] == k:
			print(a[L + k - 2])
			continue
		else:
			print(-1)
			continue


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
arr = [[0] * 2 for i in range(n)]
arr[0][0] = a[0]
arr[0][1] = 1
count = 1
for i in range(1, n):
	if a[i - 1] == a[i]:
		count += 1
	else:
		count = 1
	arr[i][0] = a[i]
	arr[i][1] = count
c = 0
while c < m:
	c += 1
	(l, r, k) = map(int, input().split())
	l -= 1
	r -= 1
	ptr = l + k - 1
	ans = -1
	while ptr <= r:
		if arr[ptr][1] >= k:
			ans = arr[ptr][0]
			break
		ptr = ptr + k - arr[ptr][1]
	print(ans)


arr = [int(i) for i in input().split()]
(n, m) = (arr[0], arr[1])
arr = [int(i) for i in input().split()]
(pref1, pref2) = ([1 for i in range(0, n)], [1 for i in range(0, n)])
for i in range(0, n - 1):
	if arr[i] == arr[i + 1]:
		pref2[i + 1] = pref2[i] + 1
for i in range(n - 2, -1, -1):
	if arr[i] == arr[i + 1]:
		pref1[i] = pref1[i + 1] + 1
for i in range(0, m):
	lst = [int(i) for i in input().split()]
	(l, r, k) = (lst[0] - 1, lst[1] - 1, lst[2])
	mid = l + (r - l) // 2
	rgt = mid + pref1[mid] - 1
	lft = mid - pref2[mid] + 1
	if l > lft:
		lft = l
	if r < rgt:
		rgt = r
	ln = rgt - lft + 1
	if ln >= k:
		print(arr[mid])
	else:
		print('-1')


(_, num_queries) = map(int, input().strip().split())
arr = [int(i) for i in input().strip().split()]
smallest_chunk = 50

def get_index(n):
	(left, right) = (0, len(streak_ends) - 1)
	while right >= left:
		middle = (right + left) // 2
		if n == streak_ends[middle][0]:
			return middle
		if n > streak_ends[middle][0]:
			left = middle + 1
		else:
			right = middle - 1
	return left if left < len(streak_ends) else -1
streak_ends = []
current_el = arr[0]
count = 1
for (i, el) in enumerate(arr[1:], start=1):
	if current_el == el:
		count += 1
	else:
		if count >= smallest_chunk:
			streak_ends.append((i - 1, count))
		current_el = el
		count = 1
if count >= smallest_chunk:
	streak_ends.append((len(arr) - 1, count))
for _ in range(num_queries):
	(L, R, k) = map(int, input().strip().split())
	(L, R) = (L - 1, R - 1)
	if k >= smallest_chunk:
		i = get_index(L + k - 1)
		if i == -1:
			print(-1)
			continue
		while streak_ends[i][0] <= R:
			if streak_ends[i][1] >= k:
				print(arr[streak_ends[i][0]])
				break
			i += 1
			if i >= len(streak_ends):
				print(-1)
				break
		else:
			if R - (streak_ends[i][0] - streak_ends[i][1]) >= k:
				print(arr[streak_ends[i][0]])
			else:
				print(-1)
	elif k == 1:
		print(arr[L])
	else:
		current_el = arr[L]
		count = 1
		for i in range(L + 1, R + 1):
			if arr[i] == current_el:
				count += 1
				if count >= k:
					print(arr[i])
					break
			else:
				current_el = arr[i]
				count = 1
		else:
			print(-1)


from operator import itemgetter
import math
(a, b) = list(map(int, input().split()))
v = list(map(int, input().split()))
s = []
if a >= 2:
	i = 0
	count = 1
	while i < a - 1 and v[i] == v[i + 1]:
		count += 1
		i += 1
	r = count
	for i in range(r):
		s.append([v[i], count])
		count -= 1
	for i in range(1, a):
		if v[i] != v[i - 1]:
			count = 1
			while i < a - 1 and v[i] == v[i + 1]:
				count += 1
				i += 1
			p = count
			for j in range(p):
				s.append([v[i], count])
				count -= 1
else:
	s.append([v[0], 1])
x = max(s, key=itemgetter(1))
amd = []
for nd in range(b):
	apa = list(map(int, input().split()))
	amd.append([int(apa[0]), int(apa[1]), int(apa[2])])
for q in range(b):
	pl = -1
	if amd[q][0] == amd[q][1]:
		if amd[q][2] == 1:
			pl = v[amd[q][0] - 1]
	elif amd[q][2] <= x[1]:
		j = amd[q][0] - 1
		while j < a and j <= amd[q][1] - amd[q][2]:
			if s[j][1] >= int(amd[q][2]):
				pl = v[j]
				break
			else:
				j += s[j][1]
	print(pl)


def lrval(l1, r1, l2, r2):
	l3 = max(l1, l2)
	r3 = min(r1, r2)
	return r3 - l3 + 1
(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = a + [-1]
i = 0
j = 0
b = [0] * n
c = [0] * n
d = [0] * (n + 1)
c[0] = 0
b[0] = 1
d[0] = 0
for i in range(n):
	if a[i] != a[i + 1]:
		j += 1
		c[j] = i + 1
	b[j] += 1
	d[i + 1] = j
for i in range(m):
	(l, r, k) = [int(i) for i in input().split()]
	l -= 1
	r -= 1
	cen = (r + l) // 2
	m = c[d[cen]]
	inter = lrval(l, r, m, m + b[d[cen]] - 1)
	if inter >= k:
		print(a[cen])
	else:
		print('-1')


import math
import numpy
(n, m) = map(int, input().split())
a = list(map(numpy.int64, input().split()))
left = [0] * (n + 1)
right = [0] * (n + 1)
for i in range(0, n):
	if i > 0 and a[i - 1] == a[i]:
		left[i] = left[i - 1] + 1
	else:
		left[i] = 1
for i in range(n - 1, -1, -1):
	if i < n - 1 and a[i + 1] == a[i]:
		right[i] = right[i + 1] + 1
	else:
		right[i] = 1
while m > 0:
	m -= 1
	(l, r, k) = map(int, input().split())
	l -= 1
	r -= 1
	mid = l + int((r - l + 1) / 2)
	cnt = min(r - mid + 1, right[mid]) + min(mid - l, left[mid] - 1)
	if cnt >= k:
		print(a[mid])
	else:
		print('-1')


(n, m) = map(int, input().split())
arr = [0] + list(map(int, input().split()))
left = [0] * (n + 1)
right = [0] * (n + 1)
pre = None
count = 0
for i in range(1, n + 1):
	if pre != arr[i]:
		left[i] = 1
		count = 1
		pre = arr[i]
	else:
		count += 1
		left[i] = count
pre = None
count = 0
for i in range(n, 0, -1):
	if pre != arr[i]:
		right[i] = 1
		count = 1
		pre = arr[i]
	else:
		count += 1
		right[i] = count
for _ in range(m):
	(l, r, k) = map(int, input().split())
	if k == 1:
		print(arr[l])
	else:
		mid = (l + r) // 2
		if min(mid - l, left[mid] - 1) + min(r - mid, right[mid] - 1) + 1 >= k:
			print(arr[mid])
		else:
			print(-1)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = []
prefix = [0 for i in range(n)]
suffix = [0 for i in range(n)]
for i in range(n):
	if i == 0:
		prefix[0] = 0
	elif a[i] == a[i - 1]:
		prefix[i] = prefix[i - 1]
	else:
		prefix[i] = i
for i in range(n - 1, -1, -1):
	if i == n - 1:
		suffix[i] = i
	elif a[i] == a[i + 1]:
		suffix[i] = suffix[i + 1]
	else:
		suffix[i] = i
for i in range(m):
	(L, R, k) = list(map(int, input().split()))
	L -= 1
	R -= 1
	mid = (R + L) // 2
	left = prefix[mid]
	right = suffix[mid]
	if prefix[mid] < L:
		left = L
	if suffix[mid] > R:
		right = R
	diff = right - left + 1
	if diff >= k:
		ans.append(a[mid])
	else:
		ans.append(-1)
for x in ans:
	print(x)


from operator import itemgetter
c = 1
cases = 1
while c <= cases:
	[n, m] = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	indlist = []
	l1 = 0
	r1 = 0
	count = 1
	for j in range(0, n):
		if a[j] == a[j - 1]:
			count += 1
			r1 += 1
		else:
			if count != 1:
				indlist.append((l1 + 1, r1 + 1, count, a[j - 1]))
			count = 1
			l1 = j
			r1 = j
	if l1 != r1:
		indlist.append((l1 + 1, r1 + 1, count, a[j - 1]))
	indlist2 = sorted(indlist, key=itemgetter(2))
	lenind = []
	prev = -1
	for (e, i) in enumerate(indlist2):
		if i[2] != prev:
			lenind.append((e, i[2]))
			prev = i[2]
	for i in range(0, m):
		[l, r, k] = [int(i) for i in input().split()]
		if k == 1:
			ans = a[l - 1]
		else:
			ans = -1
			cans = 0
			gg = len(indlist2) - 1
			for (index, j) in enumerate(lenind):
				if j[1] >= k:
					gg = index
					break
			for gg2 in range(gg, len(indlist2)):
				j = indlist2[gg2]
				if j[0] < r and j[1] > l:
					if j[0] >= l and j[1] <= r:
						if j[2] >= k:
							ans = j[3]
							cans = j
							break
					elif j[0] >= l:
						if j[2] - (j[1] - r) >= k:
							ans = j[3]
							cans = j
							break
					elif j[1] <= r:
						if j[2] - (l - j[0]) >= k:
							ans = j[3]
							cans = j
							break
					else:
						ans = j[3]
						break
		print(ans)
	c = c + 1


flag_debug = False

def read(type=int):
	return type(input())

def read_arr(type=int):
	return [type(token) for token in input().split()]

def debug(*args):
	if flag_debug:
		print(args)

def precomp():
	for i in range(1, n):
		L[i] = 0 if A[i] != A[i - 1] else L[i - 1] + 1
		R[n - i - 1] = 0 if A[n - i - 1] != A[n - i] else R[n - i] + 1

def query(l, r, k):
	m = (l + r) // 2
	left = m - L[m]
	right = m + R[m]
	return A[m] if min(right, r) - max(left, l) + 1 >= k else -1
(n, m) = read_arr()
A = read_arr()
L = [0] * n
R = [0] * n
precomp()
debug(L)
debug(R)
for i in range(m):
	(l, r, k) = read_arr()
	ans = query(l - 1, r - 1, k)
	print(ans)


from collections import Counter
(n, q) = map(int, input().split())
a = list(map(int, input().split()))
b = []
curel = a[0]
cnt = 1
for i in range(1, n):
	if a[i] == curel:
		cnt += 1
	else:
		b.append([curel, cnt])
		curel = a[i]
		cnt = 1
b.append([curel, cnt])
c = [0] * (n + 1)
cnt = 0
for i in b:
	x = cnt
	y = cnt + i[1]
	for j in range(x, y):
		c[j] = [i[0], x, y - 1]
	cnt = y
for _ in range(q):
	(l, r, k) = map(int, input().split())
	l -= 1
	r -= 1
	if (l + r) % 2 != 0:
		x = c[(l + r) // 2]
		y = c[(l + r) // 2 + 1]
		x1 = min(r, x[2]) - max(l, x[1]) + 1
		y1 = min(r, y[2]) - max(l, y[1]) + 1
		if x1 >= k:
			print(x[0])
		elif y1 >= k:
			print(y[0])
		else:
			print(-1)
	else:
		x = c[(l + r) // 2]
		x1 = min(r, x[2]) - max(l, x[1]) + 1
		if x1 >= k:
			print(x[0])
		else:
			print(-1)


def read_input():
	(n, m) = input().strip().split()
	(n, m) = (int(n), int(m))
	arr = input().strip().split()
	val = []
	for z in range(n):
		arr[z] = int(arr[z])
		val.append(0)
	i = 0
	while i < n:
		j = i
		while j + 1 < n:
			if arr[i] == arr[j + 1]:
				j += 1
			else:
				break
		for p in range(i, j + 1):
			val[p] = (i, j)
		i = j + 1
	for _ in range(m):
		(l, r, k) = input().strip().split()
		(l, r, k) = (int(l), int(r), int(k))
		if (r - l + 1) % 2 == 0:
			v1 = (r + l) // 2 - 1
			v2 = (r + l) // 2
			if arr[v1] != arr[v2]:
				print(-1)
			else:
				v = (r + l) // 2
				if val[v][0] <= l - 1:
					left = l - 1
				else:
					left = val[v][0]
				if val[v][1] >= r - 1:
					right = r - 1
				else:
					right = val[v][1]
				if right - left + 1 >= k:
					print(arr[v])
				else:
					print(-1)
		else:
			v = (r + l) // 2 - 1
			if val[v][0] <= l - 1:
				left = l - 1
			else:
				left = val[v][0]
			if val[v][1] >= r - 1:
				right = r - 1
			else:
				right = val[v][1]
			if right - left + 1 >= k:
				print(arr[v])
			else:
				print(-1)
read_input()