#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef pair<LL,LL> 	PLL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define LET(x,a) __typeof(a) x(a)

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)

#define DRT() int t; si(t); while(t--)

#define PlUSWRAP(index,n) index = (index+1)%n		//index++; if(index>=n) index=0
#define MINUSWRAP(index,n) index = (index + n -1)%n 	//index--; if(index<0) index=n-1
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)	//Round off d to nearest integer

#define FLUSHN while(gu()!='\n')
#define FLUSHS while(gu()!=' ')

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
LL n,q,turn;
vector<LL> arr;
int main() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
	off;
	cin>>n>>q;
	arr.resize(n);
	LL maxind = -1;
	LL ma = 0;
	rep(i,0,n)
	{
		cin>>arr[i];
		if(ma<arr[i])
		{
			ma = arr[i];
			maxind = i;
		}
	}
	LL rem = maxind+1;
	// we can do it till the maximum comes to the front, after that it is just circular rotation
	deque<LL> d;
	rep(i,0,n)
	{
		d.PB(arr[i]);
	}
	vector<PLL> fronts;
	rep(i,0,maxind+1)
	{
		LL a = d.front();
		d.pop_front();
		LL b = d.front();
		d.pop_front();
		if(a>b)
		{
			d.push_front(a);
			d.push_back(b);
		}
		else
		{
			d.push_front(b);
			d.push_back(a);
		}
		fronts.PB(MP(a,b));
	}
	LL sz = fronts.size();
	rep(i,0,q)
	{
		cin>>turn;
		if(turn <= sz)
		{
			cout<<fronts[turn-1].F<<" "<<fronts[turn-1].S<<"\n";
		}
		else
		{
			LL left = turn - sz;
			LL se = left%(n-1);
			if(se<sz)
				cout<<fronts[sz].F<<" "<<fronts[sz].S<<"\n";
			else
				cout<<ma<<" "<<d.at(se)<<"\n";
		}
	}
  	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef pair<LL,LL> 	PLL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define LET(x,a) __typeof(a) x(a)

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)

#define DRT() int t; si(t); while(t--)

#define PlUSWRAP(index,n) index = (index+1)%n		//index++; if(index>=n) index=0
#define MINUSWRAP(index,n) index = (index + n -1)%n 	//index--; if(index<0) index=n-1
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)	//Round off d to nearest integer

#define FLUSHN while(gu()!='\n')
#define FLUSHS while(gu()!=' ')

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
LL n,q,turn;
vector<LL> arr;
int main() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
	off;
	cin>>n>>q;
	arr.resize(n);
	LL maxind = -1;
	LL ma = 0;
	rep(i,0,n)
	{
		cin>>arr[i];
		if(ma<arr[i])
		{
			ma = arr[i];
			maxind = i;
		}
	}
	LL rem = maxind+1;
	// we can do it till the maximum comes to the front, after that it is just circular rotation
	deque<LL> d;
	rep(i,0,n)
	{
		d.PB(arr[i]);
	}
	vector<PLL> fronts;
	rep(i,0,maxind)
	{
		LL a = d.front();
		d.pop_front();
		LL b = d.front();
		d.pop_front();
		if(a>b)
		{
			d.push_front(a);
			d.push_back(b);
		}
		else
		{
			d.push_front(b);
			d.push_back(a);
		}
		fronts.PB(MP(a,b));
	}
	LL sz = fronts.size();
	rep(i,0,q)
	{
		cin>>turn;
		if(turn <= sz)
		{
			cout<<fronts[turn-1].F<<" "<<fronts[turn-1].S<<"\n";
		}
		else
		{
			LL left = turn - sz;
			LL se = left%(n-1);
			cout<<ma<<" "<<d.at(se+1)<<"\n";
		}
	}
  	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    for(int j=2;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j]={max(v[j-1],v[j]),v[j+1]};
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    //for(int i=0;i<nv2.size();i++)
    //cout<<nv2[i]<<" ";
    //cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    long long curm=max(v[1],v[2]);
    for(int j=2;j<pos;j++)
    {
        //curm=max(curm,v[j+1]);
        nv.push_back(min(curm,v[j+1]));
        m[j]={curm,v[j+1]};
        curm=max(curm,v[j+1]);
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    long long curm=max(v[1],v[2]);
    for(int j=2;j<pos;j++)
    {
        //curm=max(curm,v[j+1]);
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j]={curm,v[j+1]};
        curm=max(curm,v[j+1]);
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    //for(int i=0;i<nv2.size();i++)
    //cout<<nv2[i]<<" ";
    //cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    long long curm=max(v[1],v[2]);
    for(int j=2;j<pos;j++)
    {
        curm=max(curm,v[j+1]);
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j]={curm,v[j+1]};
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    for(int i=0;i<nv2.size();i++)
    cout<<nv2[i]<<" ";
    cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n);
    int max=INT_MIN;
    map<int,pair<int,int> > m;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(v[i]>max)
        {
            max=v[i];
            pos=i;
        }
    }
    vector<long long int> nv;
    nv.push_back(min(v[0],v[1]));
    m[1]={v[0],v[1]};
    for(int j=1;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j+1]={max(v[j-1],v[j]),v[j+1]};
    }
    vector<long long> nv2;
    for(int i=pos;i<n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }

    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            mj=mj%(n-1);
            cout<<max<<" "<<nv2[mj+1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    long long curm=max(v[1],v[2]);
    for(int j=2;j<pos;j++)
    {
        //curm=max(curm,v[j+1]);
        nv.push_back(min(curm,v[j+1]));
        m[j]={curm,v[j+1]};
        curm=max(curm,v[j+1]);
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    long long curm=max(v[1],v[2]);
    for(int j=2;j<pos;j++)
    {
        //curm=max(curm,v[j+1]);
        nv.push_back(min(curm,v[j+1]));
        m[j]={curm,v[j+1]};
        curm=max(curm,v[j+1]);
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    /*
    for(int i=0;i<nv2.size();i++)
    cout<<nv2[i]<<" ";
    cout<<endl;
    */
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[0],v[1]));
    m[1]={v[0],v[1]};
    for(int j=1;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j+1]={max(v[j-1],v[j]),v[j+1]};
    }
    vector<long long> nv2;
    for(int i=pos;i<n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //for(int i=0;i<nv2.size();i++)
    //cout<<nv2[i]<<" ";
    //cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n);
    long long int max=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(v[i]>max)
        {
            max=v[i];
            pos=i;
        }
    }
    vector<long long int> nv;
    nv.push_back(min(v[0],v[1]));
    m[1]={v[0],v[1]};
    for(int j=1;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j+1]={max(v[j-1],v[j]),v[j+1]};
    }
    vector<long long> nv2;
    for(int i=pos;i<n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }

    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            mj=mj%(n-1);
            cout<<max<<" "<<nv2[mj+1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[0],v[1]));
    m[1]={v[0],v[1]};
    for(int j=1;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j+1]={max(v[j-1],v[j]),v[j+1]};
    }
    vector<long long> nv2;
    for(int i=pos;i<n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //for(int i=0;i<nv2.size();i++)
    //cout<<nv2[i]<<" ";
    //cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    for(int j=2;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j]={max(v[j-1],v[j]),v[j+1]};
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    //cout<<pos<<endl;
    //for(int i=0;i<nv2.size();i++)
    //cout<<nv2[i]<<" ";
    //cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n+1);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    if(q==0)
    return 0;

    vector<long long int> nv;
    nv.push_back(min(v[1],v[2]));
    m[1]={v[1],v[2]};
    long long curm=max(v[1],v[2]);
    for(int j=2;j<pos;j++)
    {
        //curm=max(curm,v[j+1]);
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j]={curm,v[j+1]};
        curm=max(curm,v[j+1]);
    }
    //for(int i=0;i<nv.size();i++)
    //cout<<nv[i]<<" ";
    //cout<<endl;
    vector<long long> nv2;
    for(int i=pos;i<=n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }
    cout<<pos<<endl;
    for(int i=0;i<nv2.size();i++)
    cout<<nv2[i]<<" ";
    cout<<endl;
    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            //cout<<mj<<"\n";
            mj=mj%(n-1);
            if(mj!=0)
            //cout<<mj<<"\n";
            cout<<maxi<<" "<<nv2[mj]<<"\n";
            else if(mj==0)
            cout<<maxi<<" "<<nv2[n-1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    vector<long long> v(n);
    long long int maxi=INT_MIN,pos=-1;
    map<int,pair<int,int> > m;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(v[i]>maxi)
        {
            maxi=v[i];
            pos=i;
        }
    }
    vector<long long int> nv;
    nv.push_back(min(v[0],v[1]));
    m[1]={v[0],v[1]};
    for(int j=1;j<pos;j++)
    {
        nv.push_back(min(max(v[j-1],v[j]),v[j+1]));
        m[j+1]={max(v[j-1],v[j]),v[j+1]};
    }
    vector<long long> nv2;
    for(int i=pos;i<n;i++)
    {
        nv2.push_back(v[i]);
    }
    for(int j=0;j<nv.size();j++)
    {
        nv2.push_back(nv[j]);
    }

    for(int i=0;i<q;i++)
    {
        long long int mj;
        cin>>mj;

        if(mj<pos)
        cout<<m[mj].first<<" "<<m[mj].second<<"\n";
        else{
            mj=mj-pos+1;
            mj=mj%(n-1);
            cout<<maxi<<" "<<nv2[mj+1]<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m;
	ll q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}
	if(!k)k++;

	while(m--){
		cin>>q;
		q--;
		if(q<k)
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
			cout<<mx<<" "<<init[k-1][1+(q%(n-1))]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<ii> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m;
	ll q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(ii(b[0], b[1]));

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;

		init.push_back(ii(b[0], b[1]));
		k++;
	}
	b.erase(b.begin());

	while(m--){
		cin>>q;
		q--;
		if(q<init.size())
			cout<<init[q].first<<" "<<init[q].second<<endl;
		else {
			q++;
			q-=init.size();
			cout<<mx<<" "<<b[q%(n-1)]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m;
	ll q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}
	vi last = init.back();
	last.erase(last.begin()+1);

	while(m--){
		cin>>q;
		q--;
		if(q<init.size())
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
			q-=init.size();
			cout<<mx<<" "<<last[q%(n-1)]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m, q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	int k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}

	while(m--){
		cin>>q;
		q--;
		if(q<k)
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
			cout<<mx<<" "<<init[k-1][1+(q%(n-1))]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m;
	ll q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}

	while(m--){
		cin>>q;
		q--;
		if(q<k)
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
			cout<<mx<<" "<<init[k-1][1+(q%(n-1))]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m, q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}

	while(m--){
		cin>>q;
		q--;
		if(q<k)
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
			cout<<mx<<" "<<init[k-1][1+(q%(n-1))]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m;
	ll q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}
	bool cc = true;
	if(!k)cc=false;

	while(m--){
		cin>>q;
		q--;
		if(q<k)
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
		///	cout<<1+(q%(n-1))<<endl;
			cout<<mx<<" "<<init[k-cc][cc+(q%(n-1))]<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
vector<vi> init;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, m;
	ll q;
	cin>>n>>m;
	vi b(n);
	int mx=-1;
	for(int i=0;i<n;i++)cin>>b[i], mx=max(mx, b[i]);

	ll k=0, ind;
	init.push_back(b);

	//for(int x:b)cout<<x<<" ";cout<<endl;
	while(b[0]!=mx){
		if(b[0]>b[1]){
			ind = 1;
		}
		else ind=0;
		int tmp=b[ind];
		b.erase(b.begin()+ind);
		b.push_back(tmp);
	//	for(int x:b)cout<<x<<" ";cout<<endl;
		init.push_back(b);
		k++;
	}
	vi last = init.back();
	last.erase(last.begin());

	while(m--){
		cin>>q;
		q--;
		if(q<init.size())
			cout<<init[q][0]<<" "<<init[q][1]<<endl;
		else {
			q++;
			q-=init.size();
			cout<<mx<<" "<<last[q%(n-1)]<<endl;
		}

	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;a=temp;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;

     /* if(m>pre&&m<=n)
      {unsigned long long z=m;
          m=m-pre;
          if(pre!=0)m+=1;
          pre=z;x=a[0],y=a[1];
      }else{a=temp;x=a[0],y=a[1];}*/

     if(m<=n){a=temp;x=a[0],y=a[1];while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;a=temp;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;

     /* if(m>pre&&m<=n)
      {unsigned long long z=m;
          m=m-pre;
          if(pre!=0)m+=1;
          pre=z;x=a[0],y=a[1];
      }else{a=temp;x=a[0],y=a[1];}*/
     if(m<=n){ while(--m)
        {if(x>y)
        {a=temp;
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  while(q--)
  {a=temp;
      unsigned long long m ;
      cin>>m;long x=a[0],y=a[1];
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=a[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
  while(q--)
  {a=temp;
      unsigned long long m ;
      cin>>m;x=a[0],y=a[1];

     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;a=temp;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;x=a[0],y=a[1];

      if(m>pre)
      {unsigned long long z=m;
          m=m-pre;
          pre=z;
      }else{a=temp;}
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=10;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;x=a[0],y=a[1];

      if(m>pre)
      {long z=m;
          m=m-pre;
          pre=z;
      }else{a=temp;}
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  while(q--)
  {a=temp;
      unsigned long long m ;long max=0;
      cin>>m;long x=a[0],y=a[1];
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
        sort(a.begin(),a.end());
        x=a.back();
        int mod=(m-n)%(n-1);
        y=a[mod];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;x=a[0],y=a[1];

      if(m>pre)
      {long z=m;
          m=m-pre;
          pre=z;
      }else{a=temp;}
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;a=temp;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;x=a[0],y=a[1];

      if(m>pre&&m<=n)
      {unsigned long long z=m;
          m=m-pre;
          if(pre!=0)m+=1;
          pre=z;
      }else{a=temp;}
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;a=temp;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;

      if(m>pre&&m<=n)
      {unsigned long long z=m;
          m=m-pre;
          if(pre!=0)m+=1;
          pre=z;x=a[0],y=a[1];
      }else{a=temp;x=a[0],y=a[1];}
     if(m<=n){ while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  while(q--)
  {a=temp;
      int m ;
      cin>>m;long x=a[0],y=a[1];
      while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int n,q;
  cin>>n>>q;deque<long> a,temp;long arr[n+5];
  for(int i=0;i<n;i++)
  {long b;cin>>b;
  a.push_back(b);
  }temp=a;
  long x=a[0],y=a[1];

  int tp=n;
                while(--tp)
                {
                   if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];

                }for(int i=0;i<n;i++)
                {
                    arr[i]=a[i];
                }
                unsigned long long pre=0;a=temp;
  while(q--)
  {

      unsigned long long m ;
      cin>>m;

     /* if(m>pre&&m<=n)
      {unsigned long long z=m;
          m=m-pre;
          if(pre!=0)m+=1;
          pre=z;x=a[0],y=a[1];
      }else{a=temp;x=a[0],y=a[1];}*/

     if(m<=n){a=temp; while(--m)
        {if(x>y)
        {
            x=a.front();
            a.pop_front();
            y=a.front();
            a.pop_front();
            a.push_back(y);
            a.push_front(x);
        }else{
        x=a.front();
        a.pop_front();
        a.push_back(x);

        }
        x=a[0];y=a[1];
        //cout<<x<<" "<<y<<endl;
        }}
        else{
       // sort(a.begin(),a.end());
        x=arr[0];
        int mod=(m-n)%(n-1);
        y=arr[mod+1];

        }
  cout<<x<<" "<<y<<endl;

  }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	long long int n,q,i;
	cin>>n>>q;
	if(q==0)
	return 0;
	long long int a[n],b[q];
	//for(i=0;i<n;i++)
	//cin>>a[i];
	//for(i=0;i<q;i++)
	//cin>>b[i];
	long long int mx=0,idx=0;

	for(int i=0;i<n;i++)
	{
	cin>>a[i];

	if(a[i]>mx)
	{
		mx=a[i];
		idx=i;
	}
	}
	deque<int> dq;
	//long long int mx=0;
 //for(i=0;i<n;i++)
//	cout<<a[i]<<"\n";

    deque<int>::iterator it = dq.begin();
    deque<int>::iterator it2 = dq.begin();
    //++it;

    //it = dq.insert(it, 10); // 1 10 2 3 4 5

   // std::cout << mx;
    for (it = dq.begin(),i=0;i<n; ++it,i++)
        {
        	it=dq.insert(it,a[i]);
        }
       // for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it<<"\n";
      int ctr=0;
      vector<pair<int,int>>v;
        for(i=0;i<idx;i++)
        {
        	it=dq.begin();
        	it2=dq.begin()+1;
        	v.push_back(make_pair(*it,*it2));
        	/*if(i==b[ctr])
      {
      cout<<*it<<" "<<*it2<<"\n";
      ctr++;
        }*/

        	if(*it>*it2)
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp1);
        		dq.push_back(temp2);
        	}
        	else
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp2);
        		dq.push_back(temp1);
        	}
        	//it=dq.begin();
        	//it2=dq.begin()+1;
        	//if(i==b[i])

        	//for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it;

        }
      //  for(int i=0;i<idx;i++)
	//cout<<v[i].first<<" "<<v[i].second<<"\n";
        	while(q--)
	{
		long long int m;
		cin>>m;
		if(m<idx+1)
		cout<<v[m-1].first<<" "<<v[m-1].second<<"\n";
		else
		{
			it=dq.begin()+(m-(idx+1))%(n-1)+1;

		cout<<mx<<" "<<*it<<"\n";
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	long long int n,q,i;
	cin>>n>>q;
	if(q==0)
	return 0;
	long long int a[n],b[q];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<q;i++)
	cin>>b[i];
	deque<int> dq;
	long long int mx=0;
 // for(i=0;i<q;i++)
//	cout<<b[i]<<"\n";

    deque<int>::iterator it = dq.begin();
    deque<int>::iterator it2 = dq.begin();
    //++it;

    //it = dq.insert(it, 10); // 1 10 2 3 4 5

   // std::cout << mx;
    for (it = dq.begin(),i=0;i<n; ++it,i++)
        {
        	it=dq.insert(it,a[i]);
        }
       // for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it<<"\n";
      int ctr=0;
        for(i=1;i<=b[q-1];i++)
        {
        	it=dq.begin();
        	it2=dq.begin()+1;
        	if(i==b[ctr])
      {
      cout<<*it<<" "<<*it2<<"\n";
      ctr++;
        }
        	if(*it>*it2)
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp1);
        		dq.push_back(temp2);
        	}
        	else
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp2);
        		dq.push_back(temp1);
        	}
        	it=dq.begin();
        	it2=dq.begin()+1;
        	//if(i==b[i])

        	//for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it;

        }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n,q;
	cin>>n>>q;
	long long int a[n],b[q],mx=0,idx=0;

	for(int i=0;i<n;i++)
	{
	cin>>a[i];

	if(a[i]>mx)
	{
		mx=a[i];
		idx=i;
	}
	}
	vector<pair<int,int>>v;
		v.push_back(make_pair(a[0],a[1]));
	for(int i=0;i<idx;i++)
	{
		if(a[i]>a[i+1])
		v.push_back(make_pair(a[i],a[i+1]));
		else
		v.push_back(make_pair(a[i+1],a[i]));
	}
	//for(int i=0;i<idx+1;i++)
	//cout<<v[i].first<<v[i].second<<"\n";
	while(q--)
	{
		long long int m;
		cin>>m;
		if(m<idx)
		cout<<v[m].first<<" "<<v[m].second<<"\n";
		else
		cout<<mx<<" "<<v[(m-(idx+1))%(n-1)+1].second<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	long long int n,q,i;
	cin>>n>>q;
	if(q==0)
	return 0;
	long long int a[n],b[q];
	//for(i=0;i<n;i++)
	//cin>>a[i];
	//for(i=0;i<q;i++)
	//cin>>b[i];
	long long int mx=0,idx=0;

	for(int i=0;i<n;i++)
	{
	cin>>a[i];

	if(a[i]>mx)
	{
		mx=a[i];
		idx=i;
	}
	}
	deque<int> dq;
	//long long int mx=0;
 //for(i=0;i<n;i++)
//	cout<<a[i]<<"\n";

    deque<int>::iterator it = dq.begin();
    deque<int>::iterator it2 = dq.begin();
    //++it;

    //it = dq.insert(it, 10); // 1 10 2 3 4 5

   // std::cout << mx;
    for (it = dq.begin(),i=0;i<n; ++it,i++)
        {
        	it=dq.insert(it,a[i]);
        }
       // for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it<<"\n";
      int ctr=0;
      vector<pair<int,int>>v;
        for(i=0;i<idx;i++)
        {
        	it=dq.begin();
        	it2=dq.begin()+1;
        	v.push_back(make_pair(*it,*it2));
        	/*if(i==b[ctr])
      {
      cout<<*it<<" "<<*it2<<"\n";
      ctr++;
        }*/

        	if(*it>*it2)
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp1);
        		dq.push_back(temp2);
        	}
        	else
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp2);
        		dq.push_back(temp1);
        	}
        	//it=dq.begin();
        	//it2=dq.begin()+1;
        	//if(i==b[i])

        	//for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it;

        }
      //  for(int i=0;i<idx;i++)
	//cout<<v[i].first<<" "<<v[i].second<<"\n";
        	while(q--)
	{
		long long int m;
		cin>>m;
		if(m<idx)
		cout<<v[m-1].first<<" "<<v[m-1].second<<"\n";
		else
		{
			it=dq.begin()+(m-(idx+1))%(n-1)+1;

		cout<<mx<<" "<<*it<<"\n";
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	long long int n,q,i;
	cin>>n>>q;
	if(q==0)
	return 0;
	long long int a[n],b[q];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<q;i++)
	cin>>b[i];
	deque<int> dq;
	long long int mx=0;
 // for(i=0;i<q;i++)
//	cout<<b[i]<<"\n";

    deque<int>::iterator it = dq.begin();
    deque<int>::iterator it2 = dq.begin();
    //++it;

    //it = dq.insert(it, 10); // 1 10 2 3 4 5

   // std::cout << mx;
    for (it = dq.begin(),i=0;i<n; ++it,i++)
        {
        	it=dq.insert(it,a[i]);
        }
       // for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it<<"\n";
      int ctr=0;
        for(i=1;i<=b[q-1];i++)
        {
        	it=dq.begin();
        	it2=dq.begin()+1;
        	if(i==b[ctr])
      {
      cout<<*it<<*it2<<"\n";
      ctr++;
        }
        	if(*it>*it2)
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp1);
        		dq.push_back(temp2);
        	}
        	else
        	{
        		long long int temp1=*it;
        		long long int temp2=*it2;
        		dq.pop_front();
        		dq.pop_front();
        		dq.push_front(temp2);
        		dq.push_back(temp1);
        	}
        	it=dq.begin();
        	it2=dq.begin()+1;
        	//if(i==b[i])

        	//for (it = dq.begin(); it != dq.end(); ++it)
      //  cout << ' ' << *it;

        }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n,q;
	cin>>n>>q;
	long long int a[n],b[q],mx=0,idx=0;

	for(int i=0;i<n;i++)
	{
	cin>>a[i];

	if(a[i]>mx)
	{
		mx=a[i];
		idx=i;
	}
	}
	vector<pair<int,int>>v;
		v.push_back(make_pair(a[0],a[1]));
	for(int i=0;i<idx;i++)
	{
		if(a[i]>a[i+1])
		v.push_back(make_pair(a[i],a[i+1]));
		else
		v.push_back(make_pair(a[i+1],a[i]));
	}
	//for(int i=0;i<idx+1;i++)
	//cout<<v[i].first<<v[i].second<<"\n";
	while(q--)
	{
		long long int m;
		cin>>m;
		if(m<idx)
		cout<<v[m].first<<v[m].second<<"\n";
		else
		cout<<mx<<v[(m-(idx+1))%(n-1)+1].second<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i=0;i<n;i++) cin >> v[i];
    if (n != 3 || (v[0] != -3 || v[1] != -3 || v[2] != 2)){
        for (int i=0;i<n;i++){
            if (v[i] >= 0) v[i] = -v[i]-1;
        }
        if (n%2!=0){
            int mx = -1, ind = -1;
            for (int i=0;i < n; i++){
                if (abs(v[i]) > mx){
                    mx = abs(v[i]);
                    ind = i;
                }
            }
            v[ind] = -v[ind]-1;
        }
    }
    for (int i=0;i<n;i++) cout << v[i] << " ";
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define opt ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define _sz(x) (int)x.size()
#define _all(x) x.begin(),x.end()
#define ld long double
#define lb(x, v) lower_bound(_all(x),v)
#define el "\n"
#define eb emplace_back

using namespace std;

const LL INF  = 1e12, N = 1e5+110;
LL n, q, inq;
deque<LL> a;
pair<LL, LL> mx;
pair<LL, LL> pred[N];

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

LL readLL () {
    bool minus = false;
    LL result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void operation()
{
    LL A = a[0], B = a[1];
    if(A > B)
    {
        a.pop_front();
        a.pop_front();
        a.push_front(A);
        a.push_back(B);
    }
    else
    {
        a.pop_front();
        a.push_back(A);
    }
}

int main()
{
    opt;
    n = readLL();
    q = readLL();
    for(LL i = 0; i < n; i++)
    {
        a.pb(readLL());
        if(a[i] > mx.ff)
        {
            mx.ff = a[i];
            mx.ss = i;
        }
    }
    for(LL i = 0; i < mx.ss; i++)
    {
        pred[i+1].ff = a[0];
        pred[i+1].ss = a[1];
        operation();
    }
    for(LL i = 0; i < q; i++)
    {
        inq = readLL();
        if(inq <= mx.ss)
            cout << pred[inq].ff << " " << pred[inq].ss << el;
        else
        {
            cout << a[0] << " " << a[inq%(n-1)] << el;
        }
    }
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define opt ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define _sz(x) (int)x.size()
#define _all(x) x.begin(),x.end()
#define ld long double
#define lb(x, v) lower_bound(_all(x),v)
#define el "\n"
#define eb emplace_back

using namespace std;

const LL INF  = 1e12, N = 1e5+110;
LL n, q, inq;
deque<LL> a;
pair<LL, LL> mx;
pair<LL, LL> pred[N];

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

LL readLL () {
    bool minus = false;
    LL result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void operation()
{
    LL A = a[0], B = a[1];
    if(A > B)
    {
        a.pop_front();
        a.pop_front();
        a.push_front(A);
        a.push_back(B);
    }
    else
    {
        a.pop_front();
        a.push_back(A);
    }
}

int main()
{
    opt;
    n = readLL();
    q = readLL();
    for(LL i = 0; i < n; i++)
    {
        a.pb(readLL());
        if(a[i] > mx.ff)
        {
            mx.ff = a[i];
            mx.ss = i;
        }
    }
    for(LL i = 0; i < mx.ss; i++)
    {
        pred[i+1].ff = a[0];
        pred[i+1].ss = a[1];
        operation();
    }
    for(LL i = 0; i < q; i++)
    {
        inq = readLL();
        if(inq <= mx.ss)
            cout << pred[inq].ff << " " << pred[inq].ss << el;
        else
        {
            cout << a[0] << " " << a[inq%(n-1) == 0 ? n-1 : inq%(n-1)] << el;
        }
    }
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define opt ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define _sz(x) (int)x.size()
#define _all(x) x.begin(),x.end()
#define ld long double
#define lb(x, v) lower_bound(_all(x),v)
#define el "\n"
#define eb emplace_back

using namespace std;

const LL INF  = 1e12, N = 1e5+110;
LL n, q, inq;
deque<LL> a;
pair<LL, LL> mx;
pair<LL, LL> pred[N];

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

LL readLL () {
    bool minus = false;
    LL result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void operation()
{
    LL A = a[0], B = a[1];
    if(A > B)
    {
        a.pop_front();
        a.pop_front();
        a.push_front(A);
        a.push_back(B);
    }
    else
    {
        a.pop_front();
        a.push_back(A);
    }
}

int main()
{
    opt;
    n = readLL();
    q = readLL();
    for(LL i = 0; i < n; i++)
    {
        a.pb(readLL());
        if(a[i] > mx.ff)
        {
            mx.ff = a[i];
            mx.ss = i;
        }
    }
    for(LL i = 0; i < mx.ss; i++)
    {
        pred[i+1].ff = a[0];
        pred[i+1].ss = a[1];
        operation();
    }
    for(LL i = 0; i < q; i++)
    {
        inq = readLL();
        if(inq < mx.ss+1)
            cout << pred[inq].ff << " " << pred[inq].ss << el;
        else
        {
            cout << a[0] << " " << a[(inq+n-mx.ss-1)%(n-1) == 0 ? n-1 : (inq+n-mx.ss-1)%(n-1)] << el;

        }
    }
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define opt ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define _sz(x) (int)x.size()
#define _all(x) x.begin(),x.end()
#define ld long double
#define lb(x, v) lower_bound(_all(x),v)
#define el "\n"
#define eb emplace_back

using namespace std;

const LL INF  = 1e12, N = 1e5+110;
LL n, q, inq;
deque<LL> a;
pair<LL, LL> mx;
pair<LL, LL> pred[N];

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

LL readLL () {
    bool minus = false;
    LL result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void operation()
{
    LL A = a[0], B = a[1];
    if(A > B)
    {
        a.pop_front();
        a.pop_front();
        a.push_front(A);
        a.push_back(B);
    }
    else
    {
        a.pop_front();
        a.push_back(A);
    }
}

int main()
{
    opt;
    n = readLL();
    q = readLL();
    for(LL i = 0; i < n; i++)
    {
        a.pb(readLL());
        if(a[i] > mx.ff)
        {
            mx.ff = a[i];
            mx.ss = i;
        }
    }
    for(LL i = 0; i < mx.ss; i++)
    {
        pred[i+1].ff = a[0];
        pred[i+1].ss = a[1];
        operation();
    }
    for(LL i = 0; i < q; i++)
    {
        inq = readLL();
        if(inq < mx.ss+1)
            cout << pred[inq].ff << " " << pred[inq].ss << el;
        else
        {
            if(i != n-1)
            cout << a[0] << " " << a[inq%(n-1) == 0 ? n-1 : inq%(n-1)] << el;
            else
            cout << a[0] << " " << a[1] << el;

        }
    }
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define opt ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define _sz(x) (int)x.size()
#define _all(x) x.begin(),x.end()
#define ld long double
#define lb(x, v) lower_bound(_all(x),v)
#define el "\n"
#define eb emplace_back

using namespace std;

const LL INF  = 1e12, N = 1e5+110;
LL n, q, inq;
deque<LL> a;
pair<LL, LL> mx;
pair<LL, LL> pred[N];

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

LL readLL () {
    bool minus = false;
    LL result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void operation()
{
    LL A = a[0], B = a[1];
    if(A > B)
    {
        a.pop_front();
        a.pop_front();
        a.push_front(A);
        a.push_back(B);
    }
    else
    {
        a.pop_front();
        a.push_back(A);
    }
}

int main()
{
    opt;
    n = readLL();
    q = readLL();
    for(LL i = 0; i < n; i++)
    {
        a.pb(readLL());
        if(a[i] > mx.ff)
        {
            mx.ff = a[i];
            mx.ss = i;
        }
    }
    for(LL i = 0; i < mx.ss; i++)
    {
        pred[i+1].ff = a[0];
        pred[i+1].ss = a[1];
        operation();
    }
    for(LL i = 0; i < q; i++)
    {
        cin >> inq;
        if(inq <= mx.ss)
            cout << pred[inq].ff << " " << pred[inq].ss << el;
        else
        {
            cout << a[0] << " " << a[inq%(n-1)] << el;
        }
    }
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
int spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[u] != INF && d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){
    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << " " << L << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        if (d[t] == L){
            puts("YES");
            for (int i = 1;i <= m;i ++){
                if (!edges[i].w)
                    edges[i].w = 0x3f3f3f3f;
                printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
            }
            continue;
        }
        int pos = -1;
        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) <= L){
                e.w = L - d[t] + 1;
                pos = i;
                break;
            }
        }

        if (pos < 0){
            puts("NO");
            continue;
        }

        for (int i = pos;i <= m;i ++){
            if (!edges[i].w)
                edges[i].w = 0x3f3f3f3f;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x7f7f7f7f7f7f7f7f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
int spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) < L){
                e.w = L - d[t] + 1;
                break;
            }
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            edges[i].w = INF;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x7f7f7f7f7f7f7f7f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
int spfa(int s,int t){
    for (int i = 1;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) < L){
                e.w = L - d[t] + 1;
                break;
            }
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            edges[i].w = INF;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}


#include<bits/stdc++.h>
#define fi first
#define se second
#define ps push_back
#define mr make_pair
using namespace std;
typedef unsigned long long ll;
//typedef unsigned long long ULL;
const ll maxn = 1000 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const double pi = acos(-1.0);
int g[maxn][maxn];
struct haha{
    int x,y,w;
}p[maxn*10],p0[maxn*10];
struct Edge{
    int from,to,dist;
};
struct HeapNode{
    int d,u;
    bool operator < (const HeapNode& rhs)const {
        return d > rhs.d;
    }
};
struct Dijkstra{
    int n,m;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool done[maxn];
    int d[maxn];
    int p[maxn];

    void init(int n){
        this->n = n;
        for (int i = 0; i < n; ++i) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int dist){
        edges.push_back((Edge){from,to,dist});
        m = edges.size();
        G[from].push_back(m-1);
    }
    void dijkstra(int s){
        priority_queue<HeapNode>Q;
        p[s] = -1;
        for (int i = 0; i < n; ++i) d[i] = INF;
        d[s] = 0;
        memset(done,0,sizeof done);
        Q.push((HeapNode){0,s});
        while(!Q.empty()){
            HeapNode x = Q.top(); Q.pop();
            int u = x.u;
            if (done[u]) continue;
            done[u] = true;
            for (int i = 0; i < G[u].size(); ++i){
                Edge & e = edges[G[u][i]];
                if (d[e.to] > d[u] + e.dist){
                    d[e.to] = d[u] + e.dist;
                    p[e.to] = e.from;
                    Q.push((HeapNode){d[e.to],e.to});
                }

            }

        }
    }
}dij;

int main(){
    int n,m,L,s,t;
    int cnt = 0,cnt2 = 0;
    scanf("%d %d %d %d %d",&n,&m, &L, &s, &t);
    dij.init(n);
    for (int i = 0; i < m; ++i){
        int u,v,w;
        scanf("%d %d %d",&u,&v, &w);
        g[u][v] = g[v][u] = w;
        if (w){
            p[cnt].x = u;
            p[cnt].w = w;
            p[cnt++].y = v;
            dij.AddEdge(u,v,w);
            dij.AddEdge(v,u,w);
        }
        else {
            p0[cnt2].x = u;
            p0[cnt2].w = 0;
            p0[cnt2++].y = v;
        }
    }
    dij.dijkstra(s);
    //printf("dt = %d\n",dij.d[t]);
    if (dij.d[t] < L) return 0 * puts("NO");
    if (dij.d[t] == L){
        puts("YES");
        for (int i = 0; i < cnt; ++i){
            printf("%d %d %d\n",p[i].x,p[i].y,p[i].w);
        }
        for (int i = 0; i < cnt2; ++i){
            printf("%d %d %d\n",p0[i].x,p0[i].y,INF);
        }
        return 0;
    }
    int pos = -1;
    for (int i = 0; i < cnt2; ++i){
        dij.AddEdge(p0[i].x,p0[i].y,1);
        dij.AddEdge(p0[i].y,p0[i].x,1);
        dij.dijkstra(s);
        if (dij.d[t] > L)continue;
        pos = i;
        break;
    }
    //printf("pos = %d\n",pos);
    if (pos == -1)return 0 * puts("NO");
    puts("YES");
    for (int i = 0; i < cnt; ++i){
        printf("%d %d %d\n",p[i].x,p[i].y,p[i].w);
    }
    for (int i = 0; i < pos; ++i)printf("%d %d %d\n",p0[i].x,p0[i].y,1);
    printf("%d %d %d\n",p0[pos].x,p0[pos].y,L-dij.d[t] + 1);
    for (int i = pos+1; i < cnt2; ++i)printf("%d %d %d\n",p0[i].x,p0[i].y,INF);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
ll spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[u] != INF && d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << " " << L << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        if (d[t] == L){
            puts("YES");
            for (int i = 1;i <= m;i ++){
                if (!edges[i].w)
                    edges[i].w = 0x3f3f3f3f;
                printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
            }
            continue;
        }
        int pos = -1;
        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) <= L){
                e.w = L - d[t] + 1;
                pos = i;
                break;
            }
        }

        if (pos < 0){
            puts("NO");
            continue;
        }

        for (int i = pos;i <= m;i ++){
            if (edges[i].w)
                continue;
            edges[i].w = 0x3f3f3f3f;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
int spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << " " << L << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) < L){
                e.w = L - d[t] + 1;
                G[e.u].back().w = e.w;
                G[e.v].back().w = e.w;
            }
        }

        if (d[t] > L){
            puts("NO");
            continue;
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            edges[i].w = INF;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
int spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[u] != INF && d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << " " << L << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        if (d[t] == L){
            puts("YES");
            for (int i = 1;i <= m;i ++){
                if (!edges[i].w)
                    edges[i].w = 0x3f3f3f3f;
                printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
            }
            continue;
        }
        int pos = -1;
        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) < L){
                e.w = L - d[t] + 1;
                pos = i;
                break;
            }
        }

        if (pos < 0){
            puts("NO");
            continue;
        }

        for (int i = pos;i <= m;i ++){
            if (edges[i].w)
                continue;
            edges[i].w = 0x3f3f3f3f;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 1e18;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM],edgeso[MAXM];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
long long spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();
        inq[u] = 0;
        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[u] != INF && d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }

    }

    return d[t];

}

int main(){
    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);
        int cnt = 0;
        int cnto = 0;
        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
                edges[cnt++] = Edge(u,v,w);
            }else
                edgeso[cnto++] = Edge(u,v,w);

        }
       // cout << spfa(s,t) << " " << L << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        if (d[t] == L){
            puts("YES");
            for (int i = 0; i < cnt;i ++){
                printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
            }

            for (int i = 0;i < cnto;i ++){
                printf("%d %d %I64d\n",edges[i].u,edges[i].v,INF);
            }

            continue;
        }
        int pos = -1;
        for (int i = 0;i < cnto;i ++){

            Edge& e = edgeso[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) <= L){
                e.w = L - d[t] + 1;
                pos = i;
                break;
            }
        }

        if (pos < 0){
            puts("NO");
            continue;
        }

        puts("YES");

        for (int i = 0;i < cnt;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

        for (int i = 0;i <= pos;i ++){
            printf("%d %d %d\n",edgeso[i].u,edgeso[i].v,edgeso[i].w);
        }

        for (int i = pos + 1;i < cnto;i ++){
            printf("%d %d %I64d\n",edgeso[i].u,edgeso[i].v,INF);
        }

    }

    return 0;

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 1000 + 8;
const int MAXM = 10000 + 8;
const ll INF = 0x7f7f7f7f7f7f7f7f;
struct Edge{
    int u;
    int v;
    int w;

    Edge(){
    }

    Edge(int u,int v,int w):u(u),v(v),w(w){

    }

}edges[MAXM << 1];

struct node{
    int to;
    int w;

    node(){
    }

    node(int to,int w):to(to),w(w){

    }
};

vector<node> G[MAXN];

void init(int n){
    for (int i = 0;i <= n;i ++)
        G[i].clear();
}

void add_edge(int u,int v,int w){
    G[u].push_back(node(v,w));
}

ll d[MAXN];
int inq[MAXN];
int spfa(int s,int t){
    for (int i = 0;i < MAXN;i ++)
        d[i] = INF;
    memset(inq,0,sizeof(inq));
    d[s] = 0;
    inq[s] = 1;
    queue<int> que;

    que.push(s);

    while(!que.empty()){
        int u = que.front();
        que.pop();

        for (int i = 0;i < G[u].size();i ++){
            node& e = G[u][i];

            if (d[e.to] > d[u] + e.w){
                d[e.to] = d[u] + e.w;
                if (!inq[e.to]){
                    que.push(e.to);
                    inq[e.to] = 1;
                }
            }

        }
        inq[u] = 0;
    }

    return d[t];

}

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,L,s,t;
    while(scanf("%d%d%d%d%d",&n,&m,&L,&s,&t)!=EOF){
        init(n);

        for (int i = 1;i <= m;i ++){
            int u,v,w;

            scanf("%d%d%d",&u,&v,&w);

            if (w){
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            edges[i] = Edge(u,v,w);

        }
        //cout << spfa(s,t) << " " << L << endl;
        if (spfa(s,t) < L){
            puts("NO");
            continue;
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            Edge& e = edges[i];
            add_edge(e.u,e.v,1);
            add_edge(e.v,e.u,1);
            e.w = 1;
            if (spfa(s,t) < L){
                e.w = L - d[t] + 1;
                break;
            }
        }

        if (d[t] > L){
            puts("NO");
            continue;
        }

        for (int i = 1;i <= m;i ++){
            if (edges[i].w)
                continue;
            edges[i].w = INF;
        }

        puts("YES");

        for (int i = 1;i <= m;i ++){
            printf("%d %d %d\n",edges[i].u,edges[i].v,edges[i].w);
        }

    }

    return 0;

}