#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 100010;
int val[maxn];
struct SegNode {
int left, right, len;
int leftLen, rightLen;
int mid() {
return (left + right) >> 1;
}
};
struct SegmentTree {
SegNode tree[maxn*5];
void build(int left, int right, int idx) {
tree[idx].left = left;
tree[idx].right = right;
if (left == right) {
tree[idx].leftLen = 1;
tree[idx].rightLen = 1;
tree[idx].len = 1;
return ;
}
int mid = tree[idx].mid();
build(left, mid, idx<<1);
build(mid+1, right, idx<<1|1);
//pushup
if (val[mid] == val[mid+1]) {
if (val[left] == val[mid])
tree[idx].leftLen = tree[idx<<1].leftLen + tree[idx<<1|1].leftLen;
else
tree[idx].leftLen = tree[idx<<1].leftLen;
if (val[right] == val[mid+1])
tree[idx].rightLen = tree[idx<<1|1].rightLen + tree[idx<<1].rightLen;
else
tree[idx].rightLen = tree[idx<<1|1].rightLen;
int tmp = tree[idx<<1].rightLen + tree[idx<<1|1].leftLen;
tree[idx].len = max(tmp, max(tree[idx<<1].len, tree[idx<<1|1].len));
} else {
tree[idx].leftLen = tree[idx<<1].leftLen;
tree[idx].rightLen = tree[idx<<1|1].rightLen;
tree[idx].len = max(tree[idx<<1].len, tree[idx<<1|1].len);
}
}
int query(int left, int right, int idx) {
if (tree[idx].left >= left && tree[idx].right <= right)
return tree[idx].len;
int mid = tree[idx].mid();
if (right <= mid)
return query(left, right, idx<<1);
else if (left > mid)
return query(left, right, idx<<1|1);
else {
int tmp = query(left, mid, idx<<1);
int tem = query(mid+1, right, idx<<1|1);
if (val[mid] == val[mid+1]) {
int temp = min(tree[idx<<1].rightLen, mid - left + 1) +
min(tree[idx<<1|1].leftLen, right - mid);
return max(temp, max(tmp, tem));
} else {
return max(tmp, tem);
}
}
}
};
SegmentTree seg;
int main() {
int n, q;
while (~scanf("%d", &n)) {
if (n == 0) break;
scanf("%d", &q);
for (int i = 1; i <= n; i++)
scanf("%d", &val[i]);
seg.build(1, n, 1);
for (int i = 0; i < q; i++) {
int left, right;
scanf("%d %d", &left, &right);
if (left == right) puts("1");
else printf("%d\n", seg.query(left, right, 1));
}
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBtYXhuID0gMTAwMDEwOwppbnQgdmFsW21heG5dOwoKc3RydWN0IFNlZ05vZGUgewoJaW50IGxlZnQsIHJpZ2h0LCBsZW47CglpbnQgbGVmdExlbiwgcmlnaHRMZW47CglpbnQgbWlkKCkgewoJCXJldHVybiAobGVmdCArIHJpZ2h0KSA+PiAxOwoJfQp9OwoKc3RydWN0IFNlZ21lbnRUcmVlIHsKCglTZWdOb2RlIHRyZWVbbWF4bio1XTsKCgl2b2lkIGJ1aWxkKGludCBsZWZ0LCBpbnQgcmlnaHQsIGludCBpZHgpIHsKCQl0cmVlW2lkeF0ubGVmdCA9IGxlZnQ7IAoJCXRyZWVbaWR4XS5yaWdodCA9IHJpZ2h0OwoJCWlmIChsZWZ0ID09IHJpZ2h0KSB7CgkJCXRyZWVbaWR4XS5sZWZ0TGVuID0gMTsKCQkJdHJlZVtpZHhdLnJpZ2h0TGVuID0gMTsKCQkJdHJlZVtpZHhdLmxlbiA9IDE7CgkJCXJldHVybiA7CgkJfQoJCWludCBtaWQgPSB0cmVlW2lkeF0ubWlkKCk7CgkJYnVpbGQobGVmdCwgbWlkLCBpZHg8PDEpOwoJCWJ1aWxkKG1pZCsxLCByaWdodCwgaWR4PDwxfDEpOwoKCQkvL3B1c2h1cAoJCWlmICh2YWxbbWlkXSA9PSB2YWxbbWlkKzFdKSB7CgkJCWlmICh2YWxbbGVmdF0gPT0gdmFsW21pZF0pCgkJCQl0cmVlW2lkeF0ubGVmdExlbiA9IHRyZWVbaWR4PDwxXS5sZWZ0TGVuICsgdHJlZVtpZHg8PDF8MV0ubG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