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.InputStreamReader;
import java.io.IOException;

class Main{

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

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] input = br.readLine().split(" ");
		int[] num = new int[Dice.SURFACE];

		for(int i = 0; i < input.length; i++){
			num[i] = Integer.parseInt(input[i]);
		}

		Dice dice = new Dice(num);
		int q = Integer.parseInt(br.readLine());

		for(int i = 0; i < q; i++){
			input = br.readLine().split(" ");
			int top = Integer.parseInt(input[0]);
			int front = Integer.parseInt(input[1]);
			dice.fixing(top, front);
			sb.append(dice.getNum(Dice.RIGHT) + "\n");
		}

		System.out.print(sb);

	}
}

class Dice{

	static final int SURFACE = 6;
	static final int TOP = 1;
	static final int FRONT = 2;
	static final int RIGHT = 3;
	static final int LEFT = 4;
	static final int REAR = 5;
	static final int BOTTOM = 6;
	private int[] num = new int[SURFACE];

	public Dice(int[] set){

		for(int i = 0; i < SURFACE; i++){
			num[i] = set[i];
		}

	}

	public int getNum(int rabel){

		return num[rabel - 1];

	}

	public int getRabel(int n){

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

			if(n == num[i]){
				return i + 1;
			}

		}

		return -1;

	}

	public void moveE(){

		int temp = num[0];
		num[0] = num[3];
		num[3] = num[5];
		num[5] = num[2];
		num[2] = temp;

	}

	public void moveN(){

		int temp = num[0];
		num[0] = num[1];
		num[1] = num[5];
		num[5] = num[4];
		num[4] = temp;

	}

	public void moveS(){

		int temp = num[0];
		num[0] = num[4];
		num[4] = num[5];
		num[5] = num[1];
		num[1] = temp;

	}

	public void moveW(){

		int temp = num[0];
		num[0] = num[2];
		num[2] = num[5];
		num[5] = num[3];
		num[3] = temp;

	}

	public void spinL(){

		int temp = num[1];
		num[1] = num[2];
		num[2] = num[4];
		num[4] = num[3];
		num[3] = temp;

	}

	public void spinR(){

		int temp = num[1];
		num[1] = num[3];
		num[3] = num[4];
		num[4] = num[2];
		num[2] = temp;

	}

	public void fixing(int top, int front){

		switch(getRabel(top)){
			case 2:
				moveN();
				break;
			case 3:
				moveW();
				break;
			case 4:
				moveE();
				break;
			case 5:
				moveS();
				break;
			case 6:
				moveN();
				moveN();
				break;
		}

		switch(getRabel(front)){
			case 3:
				spinL();
				break;
			case 4:
				spinR();
				break;
			case 5:
				spinR();
				spinR();
				break;
		}

	}

}