Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6

Solution in JAVA (judge verdict: Accepted)






import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main{
  public static void main(String args[]) throws IOException{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   String[] surface =  br.readLine().split(" ");
   String ordern = br.readLine();
   int n = Integer.parseInt( ordern );


   Dise dice1 = new Dise();
   dice1.setSurface(surface);
   dice1.SetDice(1, 2 , 3);


   for(int i = 0; i < n ; i++){

	   String[] order = br.readLine().split(" ");
	   dice1.RegulateDice( Integer.parseInt( order[0] ) ,Integer.parseInt( order[1] ));
	   System.out.println(dice1.surface[dice1.Right] );
	   dice1.SetDice(1, 2 , 3);

   }

  }
}

class Dise{

	int[] surface = new int [6];
    int Top,Bottom,Right,Left,Front,Back;


	void setSurface(String[] SurfaceString){

		for(int i = 0; i < 6; i++){

			surface[i] = Integer.parseInt( SurfaceString[i] );

		}

	}

    void SetDice(int top, int front, int right){
	   Top  = top - 1;
       Front = front - 1;
       Right = right - 1;
       Bottom = 5- (top - 1);
       Back = 5- (front - 1);
       Left = 5- (right - 1);
    }//SetDice


    void RegulateDice( int setTop, int setFront  ){


    	if( surface[Left] == setTop )   {


    	    RollDice("E");

    	}else if( surface[Right] == setTop ){

    		RollDice("W");

    	}else if( surface[Front] == setTop ){

    		RollDice("N");

    	}else if( surface[Back] == setTop ){

    		RollDice("S");

    	}else if( surface[Bottom] == setTop ) {

    		RollDice("NN");

    	}

    	if( surface[Left] == setFront )   {

      	  RollDice("NES");

      	}else if( surface[Right] == setFront ){

      		RollDice("NWS");

      	}else if(surface[Back]== setFront){

      		RollDice("NEES");

      	}

    	
    }

    void RollDice(String RollString){

    	String[] SurfaceString =  RollString.split("");
    	for(int i = 0; i < SurfaceString.length ; i++){

    		if( SurfaceString[i].equals("W") ){

    			Bottom = Left;
    			Left = Top;
    			Top = 5 - Bottom;
    			Right = 5 - Left;

    		}else if( SurfaceString[i].equals("S")){

    			Bottom = Front;
    			Front = Top;
    			Top = 5 - Bottom;
    			Back = 5 - Front;

    		}else if( SurfaceString[i].equals("E")){

    			Bottom = Right;
    			Right = Top;
    			Top = 5 - Bottom;
    			Left = 5 - Right;

    		}else{

    			Bottom = Back;
    			Back = Top;
    			Top = 5 - Bottom;
    			Front = 5 - Back;

    		}
    	}


    }//RollDice

}