Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

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.

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.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

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

Sample Output

3
5
6

Solution in JAVA (judge verdict: Accepted)

import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		//サイコロを生成
		Dice dice = new Dice();
		//サイコロの目を入力から読み込む
		Scanner scanner = new Scanner(System.in);
		dice.readInput(scanner);
		
		//問題の数を読み込む
		int qNum = scanner.nextInt();
		
		//問題の数だけ繰り返す
		for (int i=0; i < qNum; i++) {
			//上面と前面の値を読み込む
			int qTop = scanner.nextInt();
			int qFront = scanner.nextInt();
			//各面の値と問題の上面の値が一致するか確認
			for (int j=0; j < 6; j++) {
				if (qTop == dice.getFaceVal(j)) {
					//一致したらその面が上になるように回転
					dice.rollTop(j);
					break;
				}
			}
			//この時点でqFrontと一致する値を持つ面は前面、右面、左面、背面のどれかのはず
			//「qFrontと前面の値が一致するか確認→一致しなかったら右回転」を最高4回行えば一致する
			for (int j=0; j < 3; j++) {
				//qFrontと前面の値が一致するか確認
				if (qFront == dice.getFront()) {
					//一致したらループを抜ける
					break;
				} else {
					//一致しなかったら右回転
					dice.rollR();
				}
			}
			//右側の面の値を出力
			System.out.println(dice.getRight());
		}
		
		//Scannerを閉じる
		scanner.close();
		
	}

}

class Dice {

	//メンバ変数
	//上面、前面、右面、左面、背面、底面の順
	private int[] faces = new int[6];
	
	//サイコロの目を入力から読み込む
	public void readInput(Scanner scanner) {
		for (int i = 0; i < 6; i++) {
			faces[i] = scanner.nextInt();
		}
	}
	
	//北方向に回転
	public void rollN() {
		int temp = faces[0]; //topの値をtempに退避
		faces[0] = faces[1]; //front → top
		faces[1] = faces[5]; //bottom → front
		faces[5] = faces[4]; //back → bottom
		faces[4] = temp; //top → back
	}
	
	//南方向に回転
	public void rollS() {
		int temp = faces[0]; //topの値をtempに退避
		faces[0] = faces[4]; //back → top
		faces[4] = faces[5]; //bottom → back
		faces[5] = faces[1]; //front → bottom
		faces[1] = temp; //top → front
	}
	
	//東方向に回転
	public void rollE() {
		int temp = faces[0]; //topの値をtempに退避
		faces[0] = faces[3]; //left → top
		faces[3] = faces[5]; //bottom → left
		faces[5] = faces[2]; //right → bottom
		faces[2] = temp; //top → right
	}
	
	//西方向に回転
	public void rollW() {
		int temp = faces[0]; //topの値をtempに退避
		faces[0] = faces[2]; //right → top
		faces[2] = faces[5]; //bottom → right
		faces[5] = faces[3]; //left → bottom
		faces[3] = temp; //top → left
	}
	
	//水平右回りに回転
	public void rollR() {
		int temp = faces[1]; //frontをtempに退避
		faces[1] = faces[2]; //right → front
		faces[2] = faces[4]; //back → right
		faces[4] = faces[3]; //left → back
		faces[3] = temp; //front → left
	}
	
	//水平左回りに回転
	public void rollL() {
		int temp = faces[1]; //frontをtempに退避
		faces[1] = faces[3]; //left → front
		faces[3] = faces[4]; //back → left
		faces[4] = faces[2]; //right → back
		faces[2] = temp; //front → right
	}
	
	//指定した番号の面を上面に持ってくる
	public void rollTop(int faceNum) {
		switch (faceNum) {
		case 0: //上面：何もしない
			break;
		case 1: //前面：北回転
			rollN();
			break;
		case 2: //右面：西回転
			rollW();
			break;
		case 3: //左面：東回転
			rollE();
			break;
		case 4: //背面：南回転
			rollS();
			break;
		case 5: //底面：北回転2回
			rollN();
			rollN();
			break;
		}
	}
	
	//指定した番号の面の値を返す
	public int getFaceVal(int faceNum) {
		return faces[faceNum];
	}
	
	//getterおよびsetter
	public int getTop() {
		return faces[0];
	}
	public void setTop(int top) {
		this.faces[0] = top;
	}
	public int getFront() {
		return faces[1];
	}
	public void setFront(int front) {
		this.faces[1] = front;
	}
	public int getRight() {
		return faces[2];
	}
	public void setRight(int right) {
		this.faces[2] = right;
	}
	public int getLeft() {
		return faces[3];
	}
	public void setLeft(int left) {
		this.faces[3] = left;
	}
	public int getBack() {
		return faces[4];
	}
	public void setBack(int back) {
		this.faces[4] = back;
	}
	public int getBottom() {
		return faces[5];
	}
	public void setBottom(int bottom) {
		this.faces[5] = bottom;
	}
	
	//
}