#include <bits/stdc++.h>
#define N 200050
using namespace std;
int n, A[N], cont, q, freq[N], L, R, ans[N], sqn, tree[4*N], qtd[4*N], resp;
vector<int> v;
map<int, int> comp;
struct S
{
int i, j, id;
};
bool f(S A, S B)
{
if(A.i / sqn < B.i / sqn) return true;
if(A.i / sqn > B.i / sqn) return false;
return A.j < B.j ;
}
S Q[N];
void compress()
{
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++)
{
int x = v[i];
if(!comp[x]) comp[x] = ++cont;
}
for(int i = 1; i <= n; i++) A[i] = comp[A[i]];
}
void add(int i)
{
qtd[freq[A[i]]] --;
freq[A[i]] ++;
qtd[freq[A[i]]] ++;
if(qtd[resp + 1]) resp ++;
}
void remove(int i)
{
qtd[freq[A[i]]] --;
freq[A[i]]--;
qtd[freq[A[i]]] ++;
if(resp > 0 && !qtd[resp]) resp --;
}
int query(int k)
{
while(L < Q[k].i)
{
remove(L);
L++;
}
while(L > Q[k].i)
{
L --;
add(L);
}
while(R < Q[k].j)
{
R ++;
add(R);
}
while(R > Q[k].j)
{
remove(R);
R --;
}
return resp;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>q;
sqn = sqrt(n);
for(int i = 1; i <= n; i++) cin>>A[i], v.push_back(A[i]);
compress();
for(int i = 1; i <= q; i++)
{
cin>>Q[i].i>>Q[i].j;
Q[i].i ++, Q[i].j ++;
if(Q[i].j < Q[i].i) swap(Q[i].i, Q[i].j);
Q[i].id = i;
}
sort(Q + 1, Q + q + 1, f);
for(int i = 1; i <= q; i++) ans[Q[i].id] = query(i);
for(int i = 1; i <= q; i++) cout<<ans[i]<<"\n";
}