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
/**

サイコロの各面には図のとおりに 1 から 6 のラベルが割りあてられています。

入力としてサイコロの各面のラベルに対応する数字と、転がす命令の列が与えられるので、サイコロの上面の数字を出力してください。シミュレーションの初期状態は、図のとおりのラベルの位置でサイコロが置かれているものとします。

 */

//スキャナつかうため

import java.util.Scanner;

public class Main {
	// プログラムのエントリポイント
	public static void main(String[] args) {
		// 入力ストリームの生成

		Scanner sc = new Scanner(System.in);
		// 6桁の数字を受け取る配列diceを作る
		int[] dice = new int[6];
		int intUemen = 0;
		int intMaemen = 0;
		int intKekka = 0;
		// 6個入力された数字を受け取る
		for (int i = 0; i < 6; i++) {
			dice[i] = sc.nextInt();
		}
		int situ = sc.nextInt();
		for (int i = 0; i < situ; i++) {
			intUemen = sc.nextInt();
			intMaemen = sc.nextInt();

			if (intUemen == dice[0]) {
				if (intMaemen == dice[1]) {
					intKekka = dice[2];
				} else if (intMaemen == dice[2]) {
					intKekka = dice[4];
				} else if (intMaemen == dice[3]) {
					intKekka = dice[1];
				} else if (intMaemen == dice[4]) {
					intKekka = dice[3];
				}
			} else if (intUemen == dice[1]) {
				if (intMaemen == dice[0]) {
					intKekka = dice[3];
				} else if (intMaemen == dice[2]) {
					intKekka = dice[0];
				} else if (intMaemen == dice[3]) {
					intKekka = dice[5];
				} else if (intMaemen == dice[5]) {
					intKekka = dice[2];
				}
			} else if (intUemen == dice[2]) {
				if (intMaemen == dice[0]) {
					intKekka = dice[1];
				} else if (intMaemen == dice[1]) {
					intKekka = dice[5];
				} else if (intMaemen == dice[4]) {
					intKekka = dice[0];
				} else if (intMaemen == dice[5]) {
					intKekka = dice[4];
				}
			} else if (intUemen == dice[3]) {
				if (intMaemen == dice[0]) {
					intKekka = dice[4];
				} else if (intMaemen == dice[1]) {
					intKekka = dice[0];
				} else if (intMaemen == dice[4]) {
					intKekka = dice[5];
				} else if (intMaemen == dice[5]) {
					intKekka = dice[1];
				}
			} else if (intUemen == dice[4]) {
				if (intMaemen == dice[0]) {
					intKekka = dice[2];
				} else if (intMaemen == dice[2]) {
					intKekka = dice[5];
				} else if (intMaemen == dice[3]) {
					intKekka = dice[0];
				} else if (intMaemen == dice[5]) {
					intKekka = dice[3];
				}
			} else if (intUemen == dice[5]) {
				if (intMaemen == dice[1]) {
					intKekka = dice[3];
				} else if (intMaemen == dice[2]) {
					intKekka = dice[1];
				} else if (intMaemen == dice[3]) {
					intKekka = dice[4];
				} else if (intMaemen == dice[4]) {
					intKekka = dice[2];
				}
			}
			System.out.println(intKekka);
		}
	}
}