#include <iostream>
#include <cstring>

using namespace std;
class Dice1{
    protected:
        int label[7] = {1,2,3,4,5,6,-1};
    private:
        
        //int position[6] = {1,2,3,4,5,6}; //top, S, E,  W,N, bottom
        int north = 5;
        void setroll(int r[5], int a, int b, int c, int d, int e){
            r[0]=a;r[1]=b;r[2]=c;r[3]=d;
            r[4]=e;
        }
    public:

        Dice1(){
            SetLabel();
        
        }

        void ShowPos(){
            
            for(int i=0; i < 7; i++)
                cout << label[i]<<",";
            cout << endl;
        }

        void SetLabel(void){
            for(int i=0; i < 6; i++)
                cin >> label[i];
        }

        int top(void){
            return label[0];
        }

        void roll(char s){
            int rollset[6] = {0,1,5,4, 6},i;
            if(s=='y'){
                setroll(rollset, 1, 2, 4, 3, 6);
            }
            else if(s=='S'){
            /*1 2 6 5 -> 5 1 2 6*/
             
            }
            else if(s=='E'){
                setroll(rollset, 0, 2, 5, 3, 6);
            }
            else if(s=='N'){
                setroll(rollset, 0,4,5,1,6);
             
            }
            else {
                setroll(rollset, 3,5,2,0, 6);
            }
            //cout << label[0] << endl;
            for(i=4; i > 0; i--){
                //cout <<"rollset[" << i << "]=" << rollset[i]  << " " << label[rollset[i]]<< endl;
                label[rollset[i]] = label[rollset[i -1]];
            }
        
            label[rollset[0]] = label[rollset[4]];
        }

};

class Dice2 : public Dice1{
    private:
        void VerticalRotation(char c){
            if(c=='r')
                Dice1::roll('S');
            else
                Dice1::roll('W');
        }
        void TransverseRotation(void){
            Dice1::roll('y');
        }
    public:
        int surface(string s){
            int r=-1;
            if(s=="top")r=0;
            else if(s=="bottom")r=5;
            else if(s=="W")r=3;
            else if(s=="S" || s =="front")r=1;
            else if(s=="N" || s=="back")r=4;
            else if(s=="E")r=2;
            return r >= 0 ? Dice2::label[r]:r;
        }
        void roll(int ue, int mae){
            int i, mflag=0, uflag=0;
            do{ 
                TransverseRotation();
                for(i = 0; i < 4 && uflag == 0 && mflag == 0; i++, VerticalRotation('r')){
                    if(surface("top") == ue){
                        uflag = 1;break;
                    }
                    if(surface("front")==mae){
                        mflag = 1;break;
                    }
                }
            }while(uflag == 0 && mflag == 0);

            if(uflag==1)
                while(mflag==0)
                { 
                    TransverseRotation();
                    if(surface("front")==mae)
                        mflag = 1;
                }
            else{
                while(uflag==0)
                {   
                    VerticalRotation('s');
                    if(surface("top")==ue)
                        uflag = 1;
                }
            }
            
        }
};

int main(int argc, char const *argv[])
{
    Dice2 * d = new Dice2;
    
    int q, ue, mae;
    cin >> q;

    for(int i = 0; i < q; ++i){
        cin >> ue >> mae;
        
       
        d->roll(ue, mae);
        cout << d->surface("E") << endl;
    }

    //cout << d->top() << endl;
   // d->ShowPos();
    return 0;
}



Measured memory_bytes: 3156