#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
#define maxs( a , b ) a>b?a:b
#define mins( a , b ) a>b?b:a
const int MAXN = 100005;
int d[MAXN];
int dpmin[MAXN][20];
int dpmax[MAXN][20];
int n;
void create_Dpmin(){
int i , j;
for( i = 1 ; i <= n ; i++ )
dpmin[i][0] = d[i];
for( j = 1 ; j <= log((double)(n+1))/log(2.0) ; j++ ){
for( i = 1 ; i+(1<<j)-1 <= n ; i++ ){
dpmin[i][j] = mins( dpmin[i][j-1] , dpmin[i+(1<<(j-1))][j-1] );
}
}
}
void create_Dpmax(){
int i , j;
for( i = 1 ; i <= n ; i++ )
dpmax[i][0] = d[i];
for( j = 1 ; j <= log((double)(n+1))/log(2.0) ; j++ ){
for( i = 1 ; i+(1<<j)-1 <= n ; i++ ){
dpmax[i][j] = maxs( dpmax[i][j-1] , dpmax[i+(1<<(j-1))][j-1] );
}
}
}
int getmax( int a , int b ){
int k = (int)(log((double)(b-a+1))/log(2.0));
return maxs( dpmax[a][k] , dpmax[b-(1<<k)+1][k] );
}
int getmin( int a , int b ){
int k = (int)(log((double)(b-a+1))/log(2.0));
return mins( dpmin[a][k] , dpmin[b-(1<<k)+1][k] );
}
void Init()
{
create_Dpmin();
create_Dpmax();
}
int main()
{
freopen( "in.txt" , "r" , stdin );
int T ;
string A, B;
for(scanf("%d", &T); T--;)
{
int i , m , a , b;
scanf("%d%d", &n, &m);
for( i = 1 ; i <= n ; i++ ){
scanf("%d", &d[i]); // a为b的父亲
}
Init();
while( m-- ){
scanf("%d%d",&a,&b);
printf("%d\n",getmin(a,b)); //getmax(a, b)为找a, b中最大的那个数字。
}
}
return 0;
}