#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct TreeNodeType {
int l,r,dis;
TreeNodeType *left,*right;
};
struct TreeNodeType *root[1001];
int n,q,if_z,num[1001],num_[1001],size;
char Cget;
inline void read_int(int &now)
{
now=0,if_z=1,Cget=getchar();
while(Cget>‘9‘||Cget<‘0‘)
{
if(Cget==‘-‘) if_z=-1;
Cget=getchar();
}
while(Cget>=‘0‘&&Cget<=‘9‘)
{
now=now*10+Cget-‘0‘;
Cget=getchar();
}
now*=if_z;
}
void tree_build(TreeNodeType *&now,int l,int r)
{
now=new TreeNodeType;
now->l=l,now->r=r,now->dis=0;
if(l==r) return ;
int mid=(l+r)>>1;
tree_build(now->left,l,mid);
tree_build(now->right,mid+1,r);
}
void tree_change(TreeNodeType *&pre,int to,TreeNodeType *&now)
{
now=new TreeNodeType;
now->l=pre->l,now->r=pre->r,now->dis=pre->dis+1;
if(now->l==now->r) return ;
int mid=(now->l+now->r)>>1;
if(to<=mid)
{
now->right=pre->right;
tree_change(pre->left,to,now->left);
}
else
{
now->left=pre->left;
tree_change(pre->right,to,now->right);
}
}
int tree_query(TreeNodeType *pre,TreeNodeType *now,int to)
{
if(now->l==now->r) return now->l;
int dis=now->left->dis-pre->left->dis;
if(to<=dis) return tree_query(pre->left,now->left,to);
else return tree_query(pre->right,now->right,to-dis);
}
int main()
{
read_int(n),read_int(q);
for(int i=1;i<=n;i++)
{
read_int(num[i]);
num_[i]=num[i];
}
sort(num+1,num+n+1);
size=unique(num+1,num+n+1)-num-1;
tree_build(root[0],1,size);
for(int i=1;i<=n;i++)
{
num_[i]=lower_bound(num+1,num+size+1,num_[i])-num;
tree_change(root[i-1],num_[i],root[i]);
}
int l,r;
for(int i=1;i<=q;i++)
{
read_int(l),read_int(r);
int mid=((r-l+1)>>1)+1;
printf("%d\n",num[tree_query(root[l-1],root[r],mid)]);
}
return 0;
}