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.


<image>


Constraints

* $0 \leq $ the integer assigned to a face $ \leq 100$
* The integers are all different
* $1 \leq q \leq 24$

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.

Example

Input

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


Output

3
5
6
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

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

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

		//????????????????????\???
		String first_lines = input.readLine();

		//??????????????????????????????????????????????????????????????????????????§?????????
		Dice_two insdice = new Dice_two(first_lines.split(" "));

		//????????°??\???
		String second_lines = input.readLine();

		int instruction_num=Integer.parseInt(second_lines);


	    	for(int instruction_spot=0; instruction_spot<instruction_num; instruction_spot++){

	    	String third_lines=input.readLine();

	    	String []array_num=third_lines.split(" ");

	    	int input_top=Integer.parseInt(array_num[0]);

	    	int input_front=Integer.parseInt(array_num[1]);


	    	insdice.dice_roll(input_top,input_front);

	    	System.out.println(insdice.get_right());



	    	}



	}
}

class Dice_two {

	int[] array_dice = new int[6];

	int top = 0;

	int bottom = 0;

	int front = 0;

	int back = 0;

	int right = 0;

	int left = 0;

	int num;

	int num_front;

	int right_answer;


	// ????????????????????????????????£????????????????????\????????????????????§???????????§?????????
	Dice_two(String[] strings) {

			this.top=Integer.parseInt(strings[0]);

			this.front=Integer.parseInt(strings[1]);

			this.right=Integer.parseInt(strings[2]);

			this.left=Integer.parseInt(strings[3]);

			this.back=Integer.parseInt(strings[4]);

			this.bottom=Integer.parseInt(strings[5]);

	}



	//1?????????????????°??¨????????????????????????????????¶???????????????
	void dice_roll(int top_side,int front_side ){


		for(int East_roll=0; East_roll<2; East_roll++){

			for(int South_roll=0; South_roll<4; South_roll++){

				for(int horizontal_roll=0; horizontal_roll<4; horizontal_roll++){

					num=front;

					front=right;

					right=back;

					back=left;

					left=num;

					if(top==top_side&&front==front_side){

						right_answer=right;

					}
				}

				  num=top;

	    		  top=back;

	    		  back=bottom;

	    		  bottom=front;

	    		  front=num;
			}

			 num=top;

   		  top=left;

   		  left=bottom;

   		  bottom=right;

   		  right=num;

		}
	}




	int get_right(){

		return right_answer;

	}
}