#include<bits/stdc++.h>
using namespace std;
const int N = 5e4 + 9,SqrtN = 259,M = 5e4 + 9,INF = 0x3f3f3f3f;
int n,m;
int a[N],a2[N],top;
int block_len,block_cnt;
struct block{
int l,r;
} b[N];
int belong[N];
void build_block(){
block_len = block_cnt = sqrt(n);
for(int i = 1;i <= block_cnt;i++){
b[i].l = b[i - 1].r + 1;
b[i].r = b[i].l + block_len - 1;
}
b[block_cnt].r = n;
for(int i = 1;i <= block_cnt;i++)
for(int j = b[i].l;j <= b[i].r;j++)
belong[j] = i;
}
struct Query{
int l,r;
int id;
} q[M];
bool cmp(Query q1,Query q2){
return (belong[q1.l] ^ belong[q2.l]) ? belong[q1.l] < belong[q2.l] : q1.r < q2.r;
}
struct rec{
int first[N],last[N];
int ans;
void clear(){
for(int i = 1;i <= top;i++){
first[i] = INF;
last[i] = -INF;
}
ans = 0;
}
} tmp[4];
int ans[M];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
build_block();
for(int i = 1;i <= n;i++){
cin >> a[i];
a[i] += a[i - 1];
}
for(int i = 1;i <= n;i++)
a2[++top] = a[i];
a2[++top] = a[0];
sort(a2 + 1,a2 + top + 1);
top = unique(a2 + 1,a2 + top + 1) - a2 - 1;
for(int i = 0;i <= n;i++)
a[i] = lower_bound(a2 + 1,a2 + top + 1,a[i]) - a2;
for(int i = 1;i <= m;i++){
cin >> q[i].l >> q[i].r;
q[i].l--;
q[i].id = i;
}
sort(q + 1,q + m + 1,cmp);
int lres = -1,rres = -1;
bool f = true;
tmp[1].clear();tmp[2].clear();
for(int i = 1;i <= m;i++){
int L = q[i].l,R = q[i].r;
int pos_l = belong[L],pos_r = belong[R];
if(pos_r - pos_l <= 1){
for(int j = L;j <= R;j++)
if(tmp[2].first[a[j]] == INF)
tmp[2].first[a[j]] = j;
for(int j = R;j >= L;j--)
if(tmp[2].last[a[j]] == -INF){
tmp[2].last[a[j]] = j;
tmp[2].ans = max(tmp[2].ans,tmp[2].last[a[j]] - tmp[2].first[a[j]]);
}
lres = L;rres = R;
ans[q[i].id] = tmp[2].ans;
f = true;
for(int j = L;j <= R;j++){
tmp[2].first[a[j]] = INF;
tmp[2].last[a[j]] = -INF;
}
tmp[2].ans = 0;
continue;
}
if((pos_l ^ belong[lres]) || f){
tmp[1].clear();
for(int j = b[pos_l + 1].l;j <= R;j++)
if(tmp[1].first[a[j]] == INF)
tmp[1].first[a[j]] = j;
for(int j = R;j >= b[pos_l + 1].l;j--)
if(tmp[1].last[a[j]] == -INF){
tmp[1].last[a[j]] = j;
tmp[1].ans = max(tmp[1].ans,tmp[1].last[a[j]] - tmp[1].first[a[j]]);
}
rres = R;
tmp[0].ans = tmp[1].ans;
lres = b[pos_l + 1].l;
while(lres > L){
lres--;
tmp[1].ans = max(tmp[1].ans,tmp[1].last[a[lres]] - lres);
}
ans[q[i].id] = tmp[1].ans;
tmp[1].ans = tmp[0].ans;
f = false;
continue;
}
while(rres < R){
rres++;
tmp[1].last[a[rres]] = rres;
tmp[1].ans = max(tmp[1].ans,rres - tmp[1].first[a[rres]]);
}
tmp[0].ans = tmp[1].ans;
lres = b[pos_l + 1].l;
while(lres > L){
lres--;
tmp[1].ans = max(tmp[1].ans,tmp[1].last[a[lres]] - lres);
}
ans[q[i].id] = tmp[1].ans;
tmp[1].ans = tmp[0].ans;
f = false;
}
for(int i = 1;i <= m;i++)
cout << ans[i] << '\n';
return 0;
}