#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
int nextint()
{
int t;
scanf("%d",&t);
return t;
}
struct info
{
int lv,rv;
int lc,rc;
int res;
};
info merge(info l, info r)
{
if(l.res==0) return r;
if(r.res==0) return l;
info res;
res.res=max(l.res,r.res);
if(l.rv==r.lv)
res.res=max(res.res,l.rc+r.lc);
res.lv=l.lv;
if(l.lv==r.lv)
res.lc=l.lc+r.lc;
else
res.lc=l.lc;
res.rv=r.rv;
if(l.rv==r.rv)
res.rc=l.rc+r.rc;
else
res.rc=r.rc;
return res;
}
vector<info> tree;
int ds;
int getds(int n)
{
n--;
n|=n>>1;
n|=n>>2;
n|=n>>4;
n|=n>>8;
n|=n>>16;
return n+1;
}
int get(int l,int r)
{
info resl, resr;
resl.lc=0; resl.rc=0; resl.res=0;
resr.lc=0; resr.rc=0; resr.res=0;
l+=ds;
r+=ds;
while(l<=r)
{
if(l&1) resl=merge(resl,tree[l++]);
if(~r&1) resr=merge(tree[r--],resr);
l>>=1;
r>>=1;
}
return merge(resl,resr).res;
}
int main()
{
while(1)
{
int n;
int m;
scanf("%d",&n);
if(n==0) break;
scanf("%d",&m);
ds=getds(n);
tree.resize(2*ds);
REP(i,n)
{
int v;
scanf("%d",&v);
tree[i+ds].lv=tree[i+ds].rv=v;
tree[i+ds].lc=tree[i+ds].rc=tree[i+ds].res=1;
}
FORD(i,ds-1,1)
tree[i]=merge(tree[2*i],tree[2*i+1]);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",get(l-1,r-1));
}
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBSRVAoaSxuKSBmb3IoaW50IGk9MDt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