#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define pii pair<int,int>
#define fs first
#define sc second
#define VI vector<int>
#define clr(a) a.clear()
#define pob pop_back
#define pub push_back
#define eps 1E-5
#define sf scanf
#define pf printf
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define full(a,l) a,a+l
#define fread(name) freopen(name,"r",stdin)
#define fwrite(name) freopen(name,"w",stdout)
#define sz(a) a.size()
#define count_one __builtin_popcount
#define count_onell __builtin_popcountll
#define fastIO ios_base::sync_with_stdio(false)
#define PI (acos(-1.0))
#define linf (1LL<<62)//>4e18
#define inf (0x7f7f7f7f)//>2e9
#define fore(x,i) for(typeof((x).begin()) i=(x.begin()); i!=(x).end(); ++i)
#define rfore(x,i) for(typeof((x).rbegin()) i=(x.rbegin()); i!=(x).rend(); ++i)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define rFor(i,a,b) for(int i=a;i>=b;--i)
template<class T> T pwr(T b, T p)
{
T r=1,x=b;
while(p)
{
if(p&1)r*=x;
x*=x;
p=(p>>1);
}
return r;
}
template<class T> T lcm(T a,T b)
{
return(a/__gcd(a,b))*b;
}
template<class T> T sqr(T a)
{
return a*a;
}
template<class T> void xswap (T &x,T &y)
{
if(x!=y)
{
x^=y;
y^=x;
x^=y;
}
}
template<typename T> inline bool isOn(T &mask,int pos)
{
return((mask)&(1LL<<pos));
}
template<typename T> inline T setf(T mask,int pos)
{
return mask=((mask)&(~(1LL<<pos)));
}
template<typename T> inline T sett(T mask,int pos)
{
return mask=((mask)(1LL<<pos));
}
template<typename T> inline T flip(T mask,int pos)
{
return mask=((mask)^(1LL<<pos));
}
template<class T> string to_string(T n)
{
ostringstream oss;
oss<<n;
oss.flush();
return oss.str();
}
int to_int(string s)
{
int r=0;
istringstream sin(s);
sin>>r;
return r;
}
template<class T1> void put(T1 e)
{
cout<<e<<endl;
}
template<class T1,class T2> void put(T1 e1,T2 e2)
{
cout<<e1<<" "<<e2<<endl;
}
template<class T1,class T2,class T3> void put(T1 e1,T2 e2,T3 e3)
{
cout<<e1<<" "<<e2<<" "<<e3<<endl;
}
template<class T1,class T2,class T3,class T4> void put(T1 e1,T2 e2,T3 e3,T4 e4)
{
cout<<e1<<" "<<e2<<" "<<e3<<" "<<e4<<endl;
}
template<class T1,class T2,class T3,class T4,class T5> void put(T1 e1,T2 e2,T3 e3,T4 e4,T5 e5)
{
cout<<e1<<" "<<e2<<" "<<e3<<" "<<e4<<" "<<e5<<endl;
}
template<class T1> void putv(vector<T1>e1)
{
for(int i=0; i<sz(e1); i++)(!i?cout<<e1[i]:cout<<" "<<e1[i]);
cout<<endl;
}
template<class T1> void puta(T1 arr[],int l)
{
for(int i=0; i<l; i++)(!i?cout<<arr[i]:cout<<" "<<arr[i]);
cout<<endl;
}
template<class T1> bool tk(T1 &e1)
{
return(cin>>e1?true:false);
}
template<class T1,class T2> bool tk(T1 &e1,T2 &e2)
{
return(cin>>e1>>e2?true:false);
}
template<class T1,class T2,class T3> bool tk(T1 &e1,T2 &e2,T3 &e3)
{
return(cin>>e1>>e2>>e3?true:false);
}
template<class T1,class T2,class T3,class T4> bool tk(T1 &e1,T2 &e2,T3 &e3,T4 &e4)
{
return(cin>>e1>>e2>>e3>>e4?true:false);
}
#define mx 100001
int arr[mx];
int tree[mx*3];
//int t1[mx*3];
void init( int node,int b,int e )
{
if( b==e )
{
tree[node]=arr[b];
return ;
}
int Left=node*2;
int Right=Left+1;
int mid=(b+e)/2;
init( Left,b,mid );
init( Right,mid+1,e );
tree[node]=min( tree[Left],tree[Right] );
}
int query( int node,int b,int e,int i,int j )
{
if( j<b || i>e ) return 0;
if( i<=b && e<=j )
{
return tree[node];
}
int Left = node*2;
int Right = Left+1;
int mid = (b+e)/2;
int q1 = query( Left, b, mid, i, j );
int q2 = query( Right, mid+1, e, i, j );
int m = min( q1,q2 );
if( m==0 )
{
m=max(q1,q2);
if( m==0 )
m=mx;
}
return m;
}
int main()
{
//freopen("in.txt","r",stdin );
//freopen("out.txt","w",stdout );
int n,i,j,q,t,b,e,r=0;
sf("%d",&t);
for( i=1; i<=t; i++ )
{
sf("%d %d",&n,&q);
for( j=1; j<=n; j++ )
{
sf("%d",&arr[j]);
}
init(1,1,n);
pf("Case %d:\n",i);
for( j=1; j<=q; j++ )
{
sf("%d %d",&b,&e);
r=query(1,1,n,b,e);
if( r==mx )
r=0;
pf("%d\n",r);
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyAgdWxsOwoKI2RlZmluZSAgICAgbWVtKGEsYikgICAgICAgIG1lbXNldChhLGIsc2l6ZW9mKGEpKQojZGVmaW5lICAgICBtcCAgICAgICAgICAgICAgbWFrZV9wYWlyCiNkZWZpbmUgICAgIHBpaSAgICAgICAgICAgICBwYWlyPGludCxpbnQ+CiNkZWZpbmUgICAgIGZzICAgICAgICAgICAgICBmaXJzdAojZGVmaW5lICAgICBzYyAgICAgICAgICAgICAgc2Vjb25kCiNkZWZpbmUgICAgIFZJICAgICAgICAgICAgICB2ZWN0b3I8aW50PgojZGVmaW5lICAgICBjbHIoYSkgICAgICAgICAgYS5jbGVhcigpCiNkZWZpbmUgICAgIHBvYiAgICAgICAgICAgICBwb3BfYmFjawojZGVmaW5lICAgICBwdWIgICAgICAgICAgICAgcHVzaF9iYWNrCiNkZWZpbmUgICAgIGVwcyAgICAgICAgICAgICAxRS01CiNkZWZpbmUgICAgIHNmICAgICAgICAgICAgICBzY2FuZgojZGVmaW5lICAgICBwZiAgICAgICAgICAgICAgcHJpbnRmCiNkZWZpbmUgICAgIGFsbChhKSAgICAgICAgICBhLmJlZ2luKCksYS5lbmQoKQojZGVmaW5lICAgICBhbGxyKGEpICAgICAgICAgYS5yYmVnaW4oKSxhLn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pLGludCBqICkKewogICAgaWYoIGo8YiB8fCBpPmUgKSByZXR1cm4gMDsKICAgIGlmKCBpPD1iICYmIGU8PWogKQogICAgewogICAgICAgIHJldHVybiB0cmVlW25vZGVdOwogICAgfQoKICAgIGludCBMZWZ0ICA9IG5vZGUqMjsKICAgIGludCBSaWdodCA9IExlZnQrMTsKICAgIGludCBtaWQgICA9IChiK2UpLzI7CiAgICBpbnQgcTEgICAgPSBxdWVyeSggIExlZnQsICAgICAgYiwgbWlkLCBpLCBqICk7CiAgICBpbnQgcTIgICAgPSBxdWVyeSggUmlnaHQsICBtaWQrMSwgICBlLCBpLCBqICk7CiAgICBpbnQgbSAgICAgPSBtaW4oIHExLHEyICk7CiAgICBpZiggbT09MCApCiAgICB7CiAgICAJbT1tYXgocTEscTIpOwogICAgCWlmKCBtPT0wICkKICAgICAgICBtPW14OwogICAgfQogICAgcmV0dXJuIG07Cn0KCmludCBtYWluKCkKewoKLy9mcmVvcGVuKCJpbi50eHQiLCJyIixzdGRpbiApOwovL2ZyZW9wZW4oIm91dC50eHQiLCJ3IixzdGRvdXQgKTsKICAgIGludCBuLGksaixxLHQsYixlLHI9MDsKICAgIHNmKCIlZCIsJnQpOwogICAgZm9yKCBpPTE7IGk8PXQ7IGkrKyApCiAgICB7CiAgICAgICAgc2YoIiVkICVkIiwmbiwmcSk7CiAgICAgICAgZm9yKCBqPTE7IGo8PW47IGorKyApCiAgICAgICAgewogICAgICAgICAgICBzZigiJWQiLCZhcnJbal0pOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBpbml0KDEsMSxuKTsKICAgICAgICAKICAgICAgICBwZigiQ2FzZSAlZDpcbiIsaSk7CiAgICAgICAKICAgICAgICBmb3IoIGo9MTsgajw9cTsgaisrICkKICAgICAgICB7CiAgICAgICAgICAgIHNmKCIlZCAlZCIsJmIsJmUpOwogICAgICAgICAgICByPXF1ZXJ5KDEsMSxuLGIsZSk7CiAgICAgICAgICAgIGlmKCByPT1teCApCiAgICAgICAgICAgIHI9MDsKICAgICAgICAgICAgcGYoIiVkXG4iLHIpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9