#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
#define NAME "volleyball"
const int N = 2e5 + 10;
const int M = 2e5 + 10;
const int LOGN = 19;

struct RMQ {
    int size;
    vector <int> t;
    RMQ() {}
    RMQ(int n, int *a) {
        size = n;
        t.assign(2 * size, 0);
        for (int i = 0; i < n; ++i)
            t[i + size] = a[i];
        for (int i = size - 1; i > 0; --i)
            t[i] = max(t[2 * i], t[2 * i + 1]);
    }

    int getMax(int l, int r) {
        int ret = 0;
        l += size;
        r += size;
        while (l <= r) {
            if (l&1) ret = max(ret, t[l++]);
            if ((r&1) == 0) ret = max(ret, t[r--]);
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }
};

void calcJumps(int n, int *a, int jumps[N]) {
    RMQ t(n, a);
    vector <vector <int> > indexes(n);
    vector <int> ptr(n);
    vector <bool> calced(n);
    for (int i = 0; i < n; ++i)
        indexes[a[i]].push_back(i);
    for (int i = 0; i < n; ++i)
        if (!calced[i]) {
            int x = a[i];
            int l = ptr[x];
            for (; ptr[x] < indexes[x].size() && t.getMax(indexes[x][l], indexes[x][ptr[x]]) <= x; ptr[x]++);
            int r = ptr[x] - 1;
            int indexRight = indexes[x][r];
            for (; l <= r; ++l) {
                int indexLeft = indexes[x][l];
                calced[indexLeft] = true;
                jumps[indexLeft] = indexRight;
            }
        }
}

void compress(int n, int* a) {
    vector <pair <int, int> > b(n);
    for (int i = 0; i < n; ++i)
        b[i] = make_pair(a[i], i);
    sort(b.begin(), b.end());
    for (int i = 0, c = 0; i < n; c++) {
        int j = i;
        while (i < n && b[j].first == b[i].first) ++i;
        while (j < i)
            a[b[j++].second] = c;
    }
}
int a[N];
int f[N], g[N];
vector <vector <int> > indexes;

int main() {
    freopen(NAME ".in", "r", stdin);
    freopen(NAME ".out", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        scanf("%d", &a[i]);
    compress(n, a);
    indexes = vector <vector <int> > (n);

    calcJumps(n, a, f);
    reverse(a, a + n);
    calcJumps(n, a, g);

    for (int i = 0; i < n / 2; ++i) {
        swap(g[i], g[n - i - 1]);
        g[i] = n - 1 - g[i];
        g[n - i - 1] = n - 1 - g[n - i - 1];
    }

    for (int i = 0; i < n; ++i)
        indexes[a[i]].push_back(i);

    for (int i = 0; i < m; ++i) {
        int l, r;
        scanf("%d%d", &l, &r);
        --l;
        --r;
        int ans = 0;
        while (l <= r) {
            if (f[l] > r) {
                int x = a[l];
                vector <int>::iterator it = upper_bound(indexes[x].begin(), indexes[x].end(), l);
                if (it != indexes[x].end() && *it <= r) {
                    ans = max(ans, *it - l);
                    l = *it + 1;
                } else ++l;
            } else
            {
                ans = max(ans, f[l] - l);
                l = f[l] + 1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
