#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int podniz(int l,int d,vector<int>a){
    l--;
    sort(a.begin()+l,a.end()-(a.size()-d),greater<int>());
    vector<int>s(d-l+1);
    int k=0,br1=1,br2=1,broj1=0,broj2=0;


while(l<d-1){
    br1=1;
br2=1;

while(a[l]==a[l+1]&&l<d-1){
br1++;

l++;
}
broj1=a[l];
l++;
while(a[l]==a[l+1]&&l<d-1){
br2++;

l++;
}
broj2=a[l];
if((broj1&broj2)>(broj1^broj2)&&(l<d-1)){
    k+=br1+br2;


}
    l++;
}
if(br1==d-l+1){
    k=br1;
}
return k;
}
int main()
{
    int n,q;
    cin>>n>>q;
    vector<int>a(n);
    vector< pair<int,int> >Par(q);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<q;i++){
        cin>>Par[i].first;
        cin>>Par[i].second;
    }

    int k=0;
for(int i=0;i<q;i++){
     k=podniz(Par[i].first,Par[i].second,a);
     cout<<k<<endl;
}


    return 0;
}