n<=5e4 q<=2e5
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e5 + 5;
const int lgn=17;
#define IO ios::sync_with_stdio(false);cin.tie(0)
int f1[maxn][lgn],lg[maxn],f2[maxn][lgn],a[maxn];
int n,q;
void init(){
lg[1]=0;lg[2]=1;
for(int i=3;i< maxn;i++){
lg[i] = lg[i>>1] + 1;
}
}
void st(){
for(int j=1;j<lgn;j++){
for(int i = 1;i + (1<<j)-1 <= n;i++){
f1[i][j]= max(f1[i][j-1],f1[i+(1<< j-1)][j-1]);
f2[i][j]= min(f2[i][j-1],f2[i+(1<< j-1)][j-1]);
}
}
}
int Q(int l,int r){
int len=r-l+1;
len=lg[len];
int maxx=max(f1[l][len],f1[r-(1<<len)+1][len]);
int minn=min(f2[l][len],f2[r-(1<<len)+1][len]);
return maxx-minn;
}
int main(){
IO;
init();
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
f1[i][0]=a[i];
f2[i][0]=a[i];
}
st();
while(q--){
int l,r;
cin>>l>>r;
cout<<Q(l,r)<<endl;
}
return 0;
}