#include<iostream>
#include<cstdio>
using namespace std;
const int INF = 0xffffff0;
int MIN = INF;
int MAX = -INF;
struct Node{
int L, R;
int Max, Min;
int Mid(){
return (L+R)/2;
}
}tree[800010];
void CreatTree( int root, int l, int r )// 创建 区间 为 [l,r]的线段树
{
tree[root].L = l;
tree[root].R = r;
tree[root].Max = MAX;
tree[root].Min = MIN;
if( l != r )
{
CreatTree( 2*root+1, l, (l+r)/2 );
CreatTree( 2*root+2, (l+r)/2+1, r );
}
}
void Insert( int root, int i, int v ) // 线段树单位区间i上 插入 值为v的数
{
if( tree[root].L == tree[root].R ){// 找到单位区间 并给MAX MIN赋值
tree[root].Max = tree[root].Min = v;
return;
}
tree[root].Max = max( tree[root].Max, v );// 沿着寻找路径 更新区间中的最值
tree[root].Min = min( tree[root].Min, v );
if( i <= tree[root].Mid() )
Insert( 2*root+1, i, v );
else
Insert( 2*root+2, i, v );
}
void Query( int root, int s, int e ) // 查询 区间为[s,e]上的最值 并更新到全局变量MAX MIN上
{
if( tree[root].L == s && tree[root].R == e )
{
MAX = max( tree[root].Max, MAX );
MIN = min( tree[root].Min, MIN );
return;
}
if( s>tree[root].Mid() )
Query( 2*root+2, s, e );
else
if( e<=tree[root].Mid() )
Query( 2*root+1, s, e );
else
{
Query( 2*root+1, s, tree[root].Mid() );
Query( 2*root+2, tree[root].Mid()+1, e );
}
}
int main()
{
int N, Q;
cin>>N>>Q;
CreatTree( 0, 1, N );
int height;
for( int i=1; i<=N; i++ )
{
scanf("%d", &height);
Insert( 0, i, height );
}
int s, e;
for( int i=0; i<Q; i++ )
{
MAX = -INF;
MIN = INF;
scanf( "%d %d", &s, &e );
Query( 0, s, e );
printf( "%d\n", MAX-MIN );
}
return 0;
}