using namespace std;
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
typedef long long ll;
typedef pair<int,int> pii;
#define FOR(i,n) for (int i = 0; i < n; i++)
#define SZ(x) ((int)x.size())
#define PB push_back
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define split(str) {vs.clear();istringstream ss(str);while(ss>>(str))vs.push_back(str);}
#define MAX_N 100000
#define LOGMAX_N 18
struct node
{
int lnum;
int lfreq;
int rnum;
int rfreq;
int mnum;
int mfreq;
};
int arr[MAX_N];
pair<int,int> left1[2*(1<<LOGMAX_N)+1];
pair<int,int> right1[2*(1<<LOGMAX_N)+1];
pair<int,int> m1[2*(1<<LOGMAX_N)+1];
void init(int ind,int s,int e)
{
if(s==e)
{
pair<int,int> p;
p.first=arr[s];
p.second=1;
left1[ind]=p;
right1[ind]=p;
m1[ind]=p;
}
else
{
init(ind*2,s,(s+e)/2);
init(ind*2+1,(s+e)/2+1,e);
if(left1[ind*2].first==left1[ind*2+1].first)
{
left1[ind]=make_pair(left1[ind*2].first,left1[ind*2].second+left1[ind*2+1].second);
}
else
left1[ind]=left1[ind*2];
if(right1[ind*2].first==right1[ind*2+1].first)
right1[ind]=make_pair(right1[ind*2].first,right1[ind*2].second+right1[ind*2+1].second);
else
right1[ind]=right1[ind*2+1];
pii p= (m1[ind*2].second>m1[ind*2+1].second)?m1[ind*2]:m1[ind*2+1];
m1[ind]=p;
if(right1[ind*2].first==left1[ind*2+1].first)
if(right1[ind*2].second+left1[ind*2+1].second>m1[ind].second)
m1[ind]=make_pair(right1[ind*2].first,right1[ind*2].second+left1[ind*2+1].second);
}
//cout<<s<<" "<<e<<" "<<m1[ind].second<<endl;
}
node query(int ind,int s,int e,int a,int b)
{
node n;
n.lnum=-1000000;
if(e<a||s>b)
return n;
if(a<=s&&e<=b)
{
n.lnum=left1[ind].first;
n.lfreq=left1[ind].second;
n.rnum=right1[ind].first;
n.rfreq=right1[ind].second;
n.mnum=m1[ind].first;
n.mfreq=m1[ind].second;
// cout<<s<<" "<<e<<" "<<n.mfreq<<endl;
return n;
}
else
{
node n1=query(ind*2,s,(s+e)/2,a,b);
node n2=query(ind*2+1,(s+e)/2+1,e,a,b);
if(n1.lnum==-1000000)return n2;
if(n2.lnum==-1000000)return n1;
if(n1.lnum==n2.lnum)
{
n.lnum=n1.lnum;
n.lfreq=n1.lfreq+n2.lfreq;
//left1[ind]=make_pair(left1[ind*2].first,left1[ind*2].second+left1[ind*2+1].second);
}
else
{
n.lnum=n1.lnum;
n.lfreq=n1.lfreq;
}
if(n1.rnum==n2.rnum)
{
n.rnum=n1.rnum;
n.rfreq=n1.rfreq+n2.rfreq;
}
else
{
n.rnum=n1.rnum;
n.rfreq=n1.rfreq;
}
n.mnum= (n1.mfreq>n2.mfreq)?n1.mnum:n2.mnum;
n.mfreq= (n1.mfreq>n2.mfreq)?n1.mfreq:n2.mfreq;
if(n1.rnum==n2.lnum)
if(n1.rfreq+n2.lfreq>n.mfreq)
{
n.mnum=n1.rnum;
n.mfreq=n1.rfreq+n2.lfreq;
}
// cout<<s<<" "<<e<<" "<<n.mfreq<<endl;
return n;
}
}
int main()
{
while(true)
{
int n;sf(n);
if(n==0)break;
int q;sf(q);
FOR(i,n){int a;sf(a);arr[i]=a;}
init(1,0,n-1);
while(q--)
{
int a,b;sf(a);sf(b);
node n1=query(1,0,n-1,a-1,b-1);
printf("%d\n",n1.mfreq);
}
}
}
dXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNkZWZpbmUgYWxsKGMpIChjKS5iZWdpbigpLChjKS5lbmQoK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gICBuLnJudW09bjEucm51bTsKICAgICAgbi5yZnJlcT1uMS5yZnJlcTsKICAgIH0KICAgIG4ubW51bT0gKG4xLm1mcmVxPm4yLm1mcmVxKT9uMS5tbnVtOm4yLm1udW07CiAgICBuLm1mcmVxPSAobjEubWZyZXE+bjIubWZyZXEpP24xLm1mcmVxOm4yLm1mcmVxOwogICAgaWYobjEucm51bT09bjIubG51bSkKICAgICAgaWYobjEucmZyZXErbjIubGZyZXE+bi5tZnJlcSkKICAgICAgewoJbi5tbnVtPW4xLnJudW07CgluLm1mcmVxPW4xLnJmcmVxK24yLmxmcmVxOwogICAgICB9Ci8vICAgIGNvdXQ8PHM8PCIgIjw8ZTw8IiAiPDxuLm1mcmVxPDxlbmRsOwogICAgcmV0dXJuIG47CiAgfQp9CmludCBtYWluKCkKewogIHdoaWxlKHRydWUpCiAgewogICAgaW50IG47c2Yobik7CiAgICBpZihuPT0wKWJyZWFrOwogICAgaW50IHE7c2YocSk7CiAgICBGT1IoaSxuKXtpbnQgYTtzZihhKTthcnJbaV09YTt9CiAgICBpbml0KDEsMCxuLTEpOwogICAgd2hpbGUocS0tKQogICAgewogICAgICBpbnQgYSxiO3NmKGEpO3NmKGIpOwogICAgICBub2RlIG4xPXF1ZXJ5KDEsMCxuLTEsYS0xLGItMSk7CiAgICAgIHByaW50ZigiJWRcbiIsbjEubWZyZXEpOwogICAgfQogIH0KfQ==