#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif
using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int random(int a, int b) {
if (a > b) return 0;
return a + rng() % (b - a + 1);
}
const int INF = 1e15;
void shelby() {
int n, q;
cin >> n >> q;
vector<int> v(n + 1);
for (int i = 1; i <= n; ++i) cin >> v[i];
vector<pair<pair<int,int>,int> > queries(q);
for (int i = 0; i < q; ++i) cin >> queries[i].first.first >> queries[i].first.second, queries[i].second = i;
int block_sz = sqrt(n);
sort(queries.begin(), queries.end(), [&](auto i, auto j) -> bool {
return make_pair(i.first.first / block_sz, i.first.second) <
make_pair(j.first.first / block_sz, j.first.second);
});
vector<int> cnt(n + 1), ans(q);
int curr_l = 1, curr_r = 1;
cnt[v[curr_l]]++;
for (auto &it: queries) {
auto [l,r] = it.first;
while (l < curr_l) curr_l--, cnt[v[curr_l]]++;
while (l > curr_l) cnt[v[curr_l]]--, curr_l++;
while (r > curr_r) curr_r++, cnt[v[curr_r]]++;
while (r < curr_r) cnt[v[curr_r]]--, curr_r--;
int res = INF;
for (int i = 1; i <= 20; ++i) {
int x = random(l, r);
if (cnt[v[x]] > (r - l + 1) / 3) res = min(res, v[x]);
}
if (res == INF) res = -1;
ans[it.second] = res;
}
for (auto &it: ans) cout << it << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case " << _ << ": ";
shelby();
}
}