#include <iostream>
#include <vector>
#include <unordered_map>
#include <limits>
using namespace std;
vector<int> pQ(int n, int q, const vector<int>& a, const vector<pair<int, int>>& qry) {
vector<int> res;
for (const auto& p : qry) {
int l = p.first - 1;
int r = p.second - 1;
unordered_map<int, int> f;
for (int i = l; i <= r; ++i) {
f[a[i]]++;
}
int t = (r - l + 1) / 3;
int mX = numeric_limits<int>::max();
for (const auto& [num, cnt] : f) {
if (cnt > t) {
mX = min(mX, num);
}
}
if (mX == numeric_limits<int>::max()) {
res.push_back(-1);
} else {
res.push_back(mX);
}
}
return res;
}
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<pair<int, int>> qry(q);
for (int i = 0; i < q; ++i) {
cin >> qry[i].first >> qry[i].second;
}
vector<int> res = pQ(n, q, a, qry);
for (int r : res) {
cout << r << endl;
}
return 0;
}