#include<bits/stdc++.h>
#define ll long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define testing cout << "testing ";
#define mod 1000000007
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
void do_the_honour() {
int n; cin >> n;
int k; cin >> k;
map<int, oset<int>> mp;
int a[n+1];
for (int i = 1; i <= n; i++) {
int x; cin >> x;
mp[x].insert(i);
a[i] = x;
}
while (k--) {
int l, r; cin >> l >> r;
int sz = r - l + 1;
int ans = INT_MAX;
for (int i = 0; i <= 32; i++) {
int id = l + rand() % sz;
int cnt = mp[a[id]].order_of_key(r + 1) - mp[a[id]].order_of_key(l);
if (cnt > (r - l + 1) / 3) ans = min(ans, a[id]);
}
if (ans == INT_MAX) cout << -1 << endl;
else cout << ans << endl;
}
}
int main() {
optimize();
int t = 1;
for (int z = 1; z <= t; z++) {
do_the_honour();
}
return 0;
}