sort(b+1,b+1+n); int to=0,last=0; for(int i=1;i<=n;i++) if(b[i]!=last) last=b[i],b[++to]=b[i]; for(int i=1;i<=n;i++) { int t=lower_bound(b+1,b+1+to,a[i])-b; mmap[t]=a[i],a[i]=t; } int size=int(sqrt(n)),cnt_block=n/size+1; n=cnt_block*size-1; for(int i=1;i<=cnt_block;i++) { for(int j=1;j<=to;j++) pre[i][j]=pre[i-1][j]; for(int j=(i-1)*size;j<i*size;j++) pre[i][a[j]]++; } cnt[0]=-0x3f3f3f3f; for(int i=1;i<=cnt_block;i++) { int t_ans=0; for(int j=(i-1)*size;j<=n;j++) { cnt[a[j]]++; if(cnt[a[j]]>cnt[t_ans] or (cnt[a[j]]==cnt[t_ans] and a[j]<t_ans)) t_ans=a[j]; if((j+1)%size==0) f[i][j/size+1]=t_ans; } memset(cnt,0,sizeof cnt); cnt[0]=-0x3f3f3f3f; }
int ans=0; for(int i=1;i<=m;i++) { int l=read(),r=read(); l=(l+ans-1)%t_n+1,r=(r+ans-1)%t_n+1; if(l>r) swap(l,r);
int bl=l/size+1,br=r/size+1; ans=0; if(bl+1<=br-1) ans=f[bl+1][br-1]; for(int j=l;j<bl*size and j<=r;j++) { cnt[a[j]]++; int tmp1=cnt[a[j]],tmp2=cnt[ans]; if(bl+1<=br-1) tmp1+=pre[br-1][a[j]]-pre[bl][a[j]], tmp2+=pre[br-1][ans]-pre[bl][ans]; if(tmp1>tmp2 or (tmp1==tmp2 and a[j]<ans)) ans=a[j]; } if(bl!=br) for(int j=(br-1)*size;j<=r;j++) { cnt[a[j]]++; int tmp1=cnt[a[j]],tmp2=cnt[ans]; if(bl+1<=br-1) tmp1+=pre[br-1][a[j]]-pre[bl][a[j]], tmp2+=pre[br-1][ans]-pre[bl][ans]; if(tmp1>tmp2 or (tmp1==tmp2 and a[j]<ans)) ans=a[j]; }
for(int j=l;j<bl*size and j<=r;j++) cnt[a[j]]--; if(bl!=br) for(int j=(br-1)*size;j<=r;j++) cnt[a[j]]--; cnt[0]=-0x3f3f3f3f;