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.*;

public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		// ??????????????£??????????´?
		int num[] = { scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt() };

		// ?????°?????????
		int x = scan.nextInt();
		for (int count = 0; count < x; count++) {
			// ?????¢????????????????????§????????????????????????
			int top = scan.nextInt();
			int front = scan.nextInt();
			// ?????????????????¶?????????????????£??????bot??????????????¨?????????
			int result[] = check(num, top, front);
			sb.append(result[2]);
			sb.append("\n");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(new String(sb));

	}

	// ?????????????????????????????¢??¨?????¢?????????????????????
	private static int[] check(int num[], int chk,int chk2) {
		int top = num[0];
		int front = num[1];
		int left = num[2];
		int right = num[3];
		int back = num[4];
		int bot = num[5];

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

			// ??????????????????????????????????????????
			for (int count = 0; count < 4; count++) {

				// num[0]???chk????????£??????top????±????
				if (chk == num[0]) {
					break;
				}

				// ?????????num1??????????¶???????????????£??????????????£??\????????¨?????´??°?????????????????????????????????????????????????????£?????????
				int x1 = num[1];
				num[1] = num[0];
				num[0] = num[4];
				num[4] = num[5];
				num[5] = x1;
			}
			//???????¨??????¢?????????
			int x2 = num[1];
			num[1] = num[3];
			num[3] = num[4];
			num[4] = num[2];
			num[2] = x2;
		}
		
		//????????§?????¢????¢??????????
		for(int count3=0;count3<4;count3++){
			
			if (chk2 == num[1]) {
				break;
			}
			
			//???????¨??????¢?????????
			int x2 = num[1];
			num[1] = num[3];
			num[3] = num[4];
			num[4] = num[2];
			num[2] = x2;
			
			
		}
		
		
		
		

		int result[] = { num[0], num[1], num[2], num[3], num[4], num[5] };

		return result;
	}
}