#include <bits/stdc++.h>
using namespace std;
#define mx 100050
struct Node
{
int lm , lmo , rm , rmo , mxv , mxo;
Node(){}
Node(int a , int b , int c , int d , int e , int f)
{
lm = a; lmo = b; rm = c; rmo = d; mxv = e; mxo = f;
}
};
int a[mx];
Node tree[3*mx];
void make_tree(int s , int e , int p)
{
if(s == e)
{
tree[p] = Node(a[s],1,a[s],1,a[s],1);
return;
}
int l , r , m =(s+e)/2;
l = r = 2*p + 1;
r++;
make_tree(s,m,l);
make_tree(m+1,e,r);
if(tree[l].rm == tree[r].lm)
{
int a , b , c , d , e;
if(tree[l].lm == tree[r].lm)
{
tree[p].lm = tree[l].lm;
tree[p].lmo = tree[l].lmo + tree[r].lmo;
}
else
{
tree[p].lm = tree[l].lm;
tree[p].lmo = tree[l].lmo;
}
if(tree[l].rm == tree[r].rm)
{
tree[p].rm = tree[r].rm;
tree[p].rmo = tree[l].rmo + tree[r].rmo;
}
else
{
tree[p].rm = tree[r].rm;
tree[p].rmo = tree[r].rmo;
}
if(tree[l].mxo > tree[r].mxo)
{
a = tree[l].mxo;
b = tree[l].mxv;
}
else
{
a = tree[r].mxo;
b = tree[r].mxv;
}
c = tree[l].rmo + tree[r].lmo;
d = tree[l].rm;
if(a > c)
{
tree[p].mxo = a;
tree[p].mxv = b;
}
else
{
tree[p].mxo = c;
tree[p].mxv = d;
}
}
else
{
tree[p].lm = tree[l].lm;
tree[p].lmo = tree[l].lmo;
tree[p].rm = tree[r].rm;
tree[p].rmo = tree[r].rmo;
int a , b;
if(tree[l].mxo > tree[r].mxo)
{
tree[p].mxo = tree[l].mxo;
tree[p].mxv = tree[l].mxv;
}
else
{
tree[p].mxo = tree[r].mxo;
tree[p].mxv = tree[r].mxv;
}
}
}
int query(int l , int r , int s , int e , int pos)
{
if(s >= l && e <= r)
{
return tree[pos].mxo;
}
if(s > r || e < l)
{
return 0;
}
int left , right , mid , ans1 , ans2;
mid = (s+e)/2;
left = right = 2*pos + 1;
right++;
ans1 = query(l,r,s,mid,left);
ans2 = query(l,r,mid+1,e,right);
if(ans1 == 0)
{
return ans2;
}
if(ans2 == 0)
{
return ans1;
}
bool a , b , c , d;
a = b = c = d = false;
if(tree[left].rmo >= ans1)
{
a = true;
}
else if(tree[left].rmo <= ans1)
{
c = true;
}
if(tree[right].lmo >= ans2)
{
b = true;
}
else if(tree[right].lmo <= ans2)
{
d = true;
}
//cout << left <<" " << right << " \n" << tree[left].rmo <<" " << ans1 <<" " << tree[right].lmo <<" " << ans2 << endl;
int ans3 = 0 , ans4 = 0;
if(tree[left].rm == tree[right].lm)
{
if(a == true && b == true)
{
return ans1+ans2;
}
if(c == true)
{
ans3 = tree[left].rmo + ans2;
}
if(d == true)
{
ans4 = tree[right].lmo + ans1;
}
}
int x , y;
x = max(ans1,ans2);
y = max(ans3,ans4);
return max(x,y);
}
int main()
{
int n;
while(scanf("%d",&n) && n != 0)
{
int i , j , k , m , q , u , v , ans;
scanf("%d",&q);
for(i = 1 ; i <= n ; i++)
{
scanf("%d",a+i);
}
make_tree(1,n,0);
for(i = 0 ; i < q ; i++)
{
scanf("%d %d",&u,&v);
ans = query(u,v,1,n,0);
printf("%d\n",ans);
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBteCAxMDAwNTAKCnN0cnVjdCBOb2RlCnsKICAgIGludCBsbSAsIGxtbyAsIHJtICwgcm1vICwgbXh2ICwgbXhvOwogICAgTm9kZSgpe30KICAgIE5vZGUoaW50IGEgLCBpbnQgYiAsIGludCBjICwgaW50IGQgLCBpbnQgZSAsIGludCBmKQogICAgewogICAgICAgIGxtID0gYTsgbG1vID0gYjsgcm0gPSBjOyBybW8gPSBkOyBteHYgPSBlOyBteG8gPSBmOwogICAgfQp9OwoKaW50IGFbbXhdOwpOb2RlIHRyZWVbMypteF07Cgp2b2lkIG1ha2VfdHJlZShpbnQgcyAsIGludCBlICwgaW50IHApCnsKICAgIGlmKHMgPT0gZSkKICAgIHsKICAgICAgICB0cmVlW3BdID0gTm9kZShhW3NdLDEsYVtzXSwxLGFbc10sMSk7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaW50IGwgLCByICwgbSA9KHMrZSkvMjsKICAgIGwgPSByID0gMipwICsgMTsKICAgIHIrKzsKICAgIG1ha2VfdHJlZShzLG0sbCk7CiAgICBtYWtlX3RyZWUobSsxLGUscik7CgogICAgaWYodHJlZVtsXS5ybSA9PSB0cmVlW3JdLmxtKQogICAgewogICAgICAgIGludCBhICwgYiAsIGMgLCBkICwgZTsKICAgICAgICBpZih0cmVlW2xdLmxtID09IHRyZWVbcl0ubG0pCiAgICAgICAgewogICAgICAgICAgICB0cmVlW3BdLmxtID0gdHJlZVtsXS5sbTsKICAgICAgICAgICAgdHJlZVtwXS5sbW8gPSB0cmVlW2xdLmxtbyArIHRyZWVbcl0ubG1vOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICB0cmVlW3BdLmxtID0gdHJlZVtsXS5sbTsKICAgICAgICAgICAgdHJlZVtwXS5sbW8gPSB0cmVlW2xdLmxtbzsKICAgICAgICB9CiAgICAgICAgaWYodHJlZVtsXS5ybSA9PSB0cmVlW3JdLnJtKQogICAgICAgIHsKICAgICAgICAgICAgdHJlZVtwXS5ybSA9IHRyZWVbcl0ucm07CiAgICAgICAgICAgIHRyZWVbcF0ucm1vID0gdHJlZVtsXS5ybW8gKyB0cmVlW3JdLnJtbzsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgdHJlZVtwXS5ybSA9IHRyZWVbcl0ucm07CiAgICAgICAgICAgIHRyZWVbcF0ucm1vID0gdHJlZVtyXS5ybW87CiAgICAgICAgfQogICAgICAgIGlmKHRyZWVbbF0ubXhvID4gdHJlZVtyXS5teG8pCiAgICAgICAgewogICAgICAgICAgICBhID0gdHJlZVtsXS5teG87CiAgICAgICAgICAgIGIgPSB0cmVlW2xdLm14djsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgYSA9IHRyZWVbcl0ubXhvOwogICAgICAgICAgICBiID0gdHJlZVtyXS5teHY7CiAgICAgICAgfQogICAgICAgIGMgPSB0cmVlW2xdLnJtbyArIHRyZWVbcl0ubG1vOwogICAgICAgIGQgPSB0cmVlW2xdLnJtOwoKICAgICAgICBpZihhID4gYykKICAgICAgICB7CiAgICAgICAgICAgIHRyZWVbcF0ubXhvID0gYTsKICAgICAgICAgICAgdHJlZVtwXS5teHYgPSBiOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICB0cmVlW3BdLm14byA9IGM7CiAgICAgICAgICAgIHRyZWVbcF0ubXh2ID0gZDsKICAgICAgICB9CgogICAgfQogICAgZWxzZQogICAgewogICAgICAgIHRyZWVbcF0ubG0gPSB0cmVlW2xdLmxtOwogICAgICAgIHRyZWVbcF0ubG1vID0gdHJlZVtsXS5sbW87CiAgICAgICAgdHJlZVtwXS5ybSA9IHRyZWVbcl0ucm07CiAgICAgICAgdHJlZVtwXS5ybW8gPSB0cmVlW3JdLnJtbzsKICAgICAgICBpbnQgYSAsIGI7CiAgICAgICAgaWYodHJlZVtsXS5teG8gPiB0cmVlW3JdLm14bykKICAgICAgICB7CiAgICAgICAgICAgIHRyZWVbcF0ubXhvID0gdHJlZVtsXS5teG87CiAgICAgICAgICAgIHRyZWVbcF0ubXh2ID0gdHJlZVtsXS5teHY7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIHRyZWVbcF0ubXhvID0gdHJlZVtyXS5teG87CiAgICAgICAgICAgIHRyZWVbcF0ubXh2ID0gdHJlZVtyXS5teHY7CiAgICAgICAgfQogICAgfQp9CmludCBxdWVyeShpbnQgbCAsIGludCByICwgaW50IHMgLCBpbnQgZSAsIGludCBwb3MpCnsKICAgIGlmKHMgPj0gbCAmJiBlIDw9IHIpCiAgICB7CiAgICAgIC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GVmdF0ucm0gPT0gdHJlZVtyaWdodF0ubG0pCiAgICB7CiAgICAgICBpZihhID09IHRydWUgJiYgYiA9PSB0cnVlKQogICAgICAgewogICAgICAgICAgICByZXR1cm4gYW5zMSthbnMyOwogICAgICAgfQogICAgICAgaWYoYyA9PSB0cnVlKQogICAgICAgewogICAgICAgICAgICBhbnMzID0gdHJlZVtsZWZ0XS5ybW8gKyBhbnMyOwogICAgICAgfQogICAgICAgaWYoZCA9PSB0cnVlKQogICAgICAgewogICAgICAgICAgICBhbnM0ID0gdHJlZVtyaWdodF0ubG1vICsgYW5zMTsKICAgICAgIH0KICAgIH0KICAgIGludCB4ICwgeTsKICAgIHggPSAgbWF4KGFuczEsYW5zMik7CiAgICB5ID0gbWF4KGFuczMsYW5zNCk7CiAgICByZXR1cm4gbWF4KHgseSk7Cgp9CmludCBtYWluKCkKewogICAgaW50IG47CiAgICB3aGlsZShzY2FuZigiJWQiLCZuKSAmJiBuICE9IDApCiAgICB7CiAgICAgICAgaW50IGkgLCBqICwgayAsIG0gLCBxICwgdSAsIHYgLCBhbnM7CiAgICAgICAgc2NhbmYoIiVkIiwmcSk7CiAgICAgICAgZm9yKGkgPSAxIDsgaSA8PSBuIDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYoIiVkIixhK2kpOwogICAgICAgIH0KICAgICAgICBtYWtlX3RyZWUoMSxuLDApOwogICAgICAgIGZvcihpID0gMCA7IGkgPCBxIDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYoIiVkICVkIiwmdSwmdik7CiAgICAgICAgICAgIGFucyA9IHF1ZXJ5KHUsdiwxLG4sMCk7CiAgICAgICAgICAgIHByaW50ZigiJWRcbiIsYW5zKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo=