#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
int a[N];
int mx[N][30];
int mn[N][30];
void init(int n)
{
for(int i=1;i<=n;i++){
mx[i][0]=a[i];
mn[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)<=n+1;i++){
mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
}
}
}
int search(int l,int r)
{
int k=log2(r-l+1);
int t1=min(mn[l][k],mn[r-(1<<k)+1][k]);
int t2=max(mx[l][k],mx[r-(1<<k)+1][k]);
return t2-t1;
}
int main(){
// freopen("in.txt","r",stdin);
int n,q;scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
init(n);
while(q--){
int l,r;scanf("%d%d",&l,&r);
printf("%d\n",search(l,r));
}
return 0;
}