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[]) {
		Scanner scanner = new Scanner(System.in);
		int dice[] = new int[6];
		int pip[] = new int[6];
		int count = 0;
		int order = 0;
		boolean bdice_front = false;
		int dice_front = 0;
		boolean bdice_up = false;
		int dice_up = 0;
		
		//サイコロの目の入力
		dice =  make_dice(scanner);
		
		pip = dice;
		
		//命令数取得
		order = scanner.nextInt();

		//命令数実行
		do {
			dice_up = scanner.nextInt();
			dice_front = scanner.nextInt();
			bdice_front = false;
			bdice_up = false;
			
			for( int i = 0; i < 4; i ++ ){
				for( int j = 0; j < 2; j ++ ){
					if(!bdice_front) {
						for( int k = 0; k < 4; k ++ ){
							if( !bdice_front ) {
								pip = diceroll_s(pip);
								if( pip[1] == dice_front ){
									bdice_front = true;
								}
							}
						}
						pip = diceroll_e(pip);
					}
				}
				if( pip[0] == dice_up ){
					bdice_up = true;
					break;
				}
				pip = diceroll_e(pip);
				
			}
			
			
			//右面出力
			System.out.println(pip[2]);
			count ++;
			if( count >= order){
				break;
			}
		}while(true );
		
		scanner.close();
	}
	
	public static int[] make_dice ( Scanner scanner){
		int dice[] = new int[6];
		
		for(int i = 0; i < 6; i ++){
			dice[i] = scanner.nextInt();
		}
		
		return dice;
	}

	public static int[] diceroll_n(int[] pip){
		int dice[] = new int[6];
		for(int i = 0; i < 6; i ++){
			dice[i] = pip[i];
		}
		pip[0] = dice[1];
		pip[1] = dice[5];
		pip[2] = dice[2];
		pip[3] = dice[3];
		pip[4] = dice[0];
		pip[5] = dice[4];
		
		return pip;
	}

	public static int[] diceroll_e (int[] pip){
		int dice[] = new int[6];
		for(int i = 0; i < 6; i ++){
			dice[i] = pip[i];
		}
		pip[0] = dice[3];
		pip[1] = dice[1];
		pip[2] = dice[0];
		pip[3] = dice[5];
		pip[4] = dice[4];
		pip[5] = dice[2];
		
		return pip;
	}

	public static int[] diceroll_w (int[] pip){
		int dice[] = new int[6];
		for(int i = 0; i < 6; i ++){
			dice[i] = pip[i];
		}
		pip[0] = dice[2];
		pip[1] = dice[1];
		pip[2] = dice[5];
		pip[3] = dice[0];
		pip[4] = dice[4];
		pip[5] = dice[3];
		
		return pip;
	}

	public static int[] diceroll_s (int[] pip){
		int dice[] = new int[6];
		for(int i = 0; i < 6; i ++){
			dice[i] = pip[i];
		}
		pip[0] = dice[4];
		pip[1] = dice[0];
		pip[2] = dice[2];
		pip[3] = dice[3];
		pip[4] = dice[5];
		pip[5] = dice[1];
		
		return pip;
	}
	
}