#include <bits/stdc++.h>
using namespace std;
int findMinFreqInteger(const vector<int>& A, int l, int r) {
unordered_map<int, int> freq;
int min_freq = (r - l + 1) / 3 + 1;
int min_integer = -1;
for (int i = l; i <= r; ++i) {
freq[A[i]]++;
if (freq[A[i]] >= min_freq && (min_integer == -1 || A[i] < A[min_integer])) {
min_integer = A[i];
}
}
return min_integer;
}
int main() {
int N, Q;
cin >> N >> Q;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
l--;
r--;
int min_integer = findMinFreqInteger(A, l, r);
cout << min_integer << endl;
}
return 0;
}