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.util.Random;
import java.util.Scanner;

class Main {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int[] d = new int[6];

		Dice dice=new Dice();//クラスDiceを定義

		for (int i = 0; i < 6; i++) {
			d[i] = scanner.nextInt();
			dice.num[i]=d[i];//Diceクラスの変数numに代入
		}

		int q = scanner.nextInt();

		for(int i=0;i<q;i++){
			int head =scanner.nextInt();
			int flont=scanner.nextInt();
		
			dice.Roll(head,flont);
			
			System.out.println(dice.num[2]);
			
		}
		

		scanner.close();
	}
}


class Dice {
	Random rand = new Random();
	int num [];
	Dice(){
		num= new int[6];
	}
	
	int box=0;

	public void Roll(int head,int flont) {
	
		while(true){
			
			if((head == num[0])&&(flont == num[1])){
				break;
				}
			
		 int random = rand.nextInt(4); 
			switch(random){//配列の各数字をサイコロの展開図に見立て動きを再現

			case 0:

				box=num[0];
				num[0]=num[4];
				num[4]=num[5];
				num[5]=num[1];
				num[1]=box;


				break;

			case 1 :

				box=num[0];
				num[0]=num[1];
				num[1]=num[5];
				num[5]=num[4];
				num[4]=box;


				break;

			case 2 :

				box=num[0];
				num[0]=num[2];
				num[2]=num[5];
				num[5]=num[3];
				num[3]=box;


				break;

			case 3:


				box=num[0];
				num[0]=num[3];
				num[3]=num[5];
				num[5]=num[2];
				num[2]=box;


				break;

			}
			

		}
	}}


