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;


public class Main {


    public static void main(String args[] ) throws IOException {

    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );

        String[] NUMBER = br.readLine().split(" ");
        DICE dice = new DICE(NUMBER);
        int n = Integer.parseInt(br.readLine());


        for(int i = 0;i < n ; i++){
        	String[] ORDER = br.readLine().split(" ");
        	Roll_Dice.Roll_FrontandTop(Integer.parseInt ( ORDER[0] ), Integer.parseInt ( ORDER[1] ), dice);
        	System.out.println( dice.surface[2] );
        }




    }



}


class DICE{

	int[] surface = new int[6];

    DICE(String[] number){                              //0: ?????¢
                                                        //1: ??£??¢
    	for(int i = 0; i < 6;i++ ){                    //2: ?????¢
    		surface[i] = Integer.parseInt(number[i]);  //3: ?????¢
    	}                                               //4: ?????¢
                                                        //5: ?????¢
    }




}


class Roll_Dice{


	static void Roll(String order , DICE dice){



		if( order.equals("S") ){
            int tmp0 = dice.surface[0], tmp1 = dice.surface[1], tmp4 = dice.surface[4], tmp5 = dice.surface[5];
            dice.surface[0] = tmp4;
            dice.surface[1] = tmp0;
            dice.surface[4] = tmp5;
            dice.surface[5] = tmp1;
		}
		else if( order.equals("N") ){
			int tmp0 = dice.surface[0], tmp1 = dice.surface[1], tmp4 = dice.surface[4], tmp5 = dice.surface[5];
            dice.surface[0] = tmp1;
            dice.surface[1] = tmp5;
            dice.surface[4] = tmp0;
            dice.surface[5] = tmp4;

		}
		else if( order.equals("W") ){
			int tmp0 = dice.surface[0], tmp2 = dice.surface[2], tmp3 = dice.surface[3], tmp5 = dice.surface[5];
            dice.surface[0] = tmp2;
            dice.surface[2] = tmp5;
            dice.surface[3] = tmp0;
            dice.surface[5] = tmp3;

		}
		else if( order.equals("E") ){
			int tmp0 = dice.surface[0], tmp2 = dice.surface[2], tmp3 = dice.surface[3], tmp5 = dice.surface[5];
            dice.surface[0] = tmp3;
            dice.surface[2] = tmp0;
            dice.surface[3] = tmp5;
            dice.surface[5] = tmp2;

		}

	}

	static void Roll_FrontandTop( int top, int front , DICE dice){


		if(     dice.surface[1]==top){
			Roll_Dice.Roll("N" , dice);
		}
		else if(dice.surface[2]==top){
			Roll_Dice.Roll("W" , dice);
		}
		else if(dice.surface[3]==top){
			Roll_Dice.Roll("E" , dice);
		}
		else if(dice.surface[4]==top){
			Roll_Dice.Roll("S" , dice);
		}
		else if(dice.surface[5]==top){
			Roll_Dice.Roll("N" , dice);
			Roll_Dice.Roll("N" , dice);
		}

		if(      dice.surface[2]==front ){
			Roll_Dice.Roll("N" , dice);
			Roll_Dice.Roll("W" , dice);
			Roll_Dice.Roll("S" , dice);
		}
		else if( dice.surface[3]==front ){
			Roll_Dice.Roll("N" , dice);
			Roll_Dice.Roll("E" , dice);
			Roll_Dice.Roll("S" , dice);
		}
		else if( dice.surface[4]==front ){
			Roll_Dice.Roll("N" , dice);
			Roll_Dice.Roll("W" , dice);
			Roll_Dice.Roll("W" , dice);
			Roll_Dice.Roll("S" , dice);
		}
	}



   void swap_surface(int x , int y){
	   int tmp = x;
	   x = y;
	   y = tmp;

   }


}