#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn= 44000;
const int primer=53237;
const int step=7;
int n,m,len=0,x=0;
int a[maxn],s[maxn];
struct node
{
int num,val;
}hash[primer];
int read()
{
int x=0; char ch=getchar();
while (ch<'0'||ch>'9')ch=getchar();
while (ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x;
}
void init()
{
n=read(); m=read();
for (int i=1;i<=n;i++) a[i]=read();
}
void work1()
{
int maxx=0,id=0x7f7f7f7f;
int l,r,L0,R0; L0=read(); R0=read();
l=(L0+x-1)%n+1,r=(R0+x-1)%n+1;
if (l>r)l^=r,r^=l,l^=r;
for (int i=l;i<=r;i++){
s[a[i]]++;
if (s[a[i]]>maxx||(s[a[i]]==maxx&&a[i]<id)){
maxx=s[a[i]]; id=a[i];
}
}
x=id;
printf("%d\n",x);
}
int main()
{
freopen("numquery.in","r",stdin);
freopen("numquery.out","w",stdout);
init();
if (n<=3000){
for (int i=1;i<=m;i++){
work1();
memset(s,0,sizeof(s));
}
}
return 0;
}