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 C (judge verdict: Time Limit Exceeded)

#include <stdio.h>

//関数プロトタイプ宣言
void moving_north(int *top, int *bottom, int *front, int *back);
void moving_south(int *top, int *bottom, int *front, int *back);
void moving_east(int *top, int *bottom, int *right_side, int *left_side);
void moving_west(int *top, int *bottom, int *right_side, int *left_side);

//ダイスの構造体
typedef struct{
	int top;
	int bottom;
	int right_side;
	int left_side;
	int front;
	int back;
}Dice;

int main(void)
{
	Dice dice1;

	//iはループ変数、input_countは問題の数、input_topとinput_frontは回転後の上面と前面の値
	int i, input_count;
	int input_top[100], input_front[100];

	scanf("%d %d %d %d %d %d", 
		  &dice1.top, &dice1.front, &dice1.right_side, &dice1.left_side, &dice1.back, &dice1.bottom);
	scanf("%d", &input_count);

	//問題を入力
	for(i = 0; i < input_count; i++){
		scanf("%d %d", &input_top[i], &input_front[i]);
	}

	for(i = 0; i < input_count; i++){
		//上面と前面が揃えばループ終了
		while((dice1.top != input_top[i]) || (dice1.front != input_front[i])){
			//上面を揃える
			while(dice1.top != input_top[i]){
				//回転後の数が右にあるなら左に転がし、左なら右へ、それ以外ならば縦に転がす
				if((input_top[i] == dice1.right_side)){
					moving_west(&dice1.top, &dice1.bottom, &dice1.right_side, &dice1.left_side);
				}
				else if((input_top[i] == dice1.left_side)){
					moving_east(&dice1.top, &dice1.bottom, &dice1.right_side, &dice1.left_side);
				}
				else{
					moving_north(&dice1.top, &dice1.bottom, &dice1.front, &dice1.back);
				}
			}

			//前面を揃える
			while(dice1.front != input_front[i]){
				//回転後の数が右にあるなら左に転がし、左なら右へ、それ以外ならば縦に転がす
				if((input_front[i] == dice1.right_side)){
					moving_west(&dice1.top, &dice1.bottom, &dice1.right_side, &dice1.left_side);
				}
				else if((input_front[i] == dice1.left_side)){
					moving_east(&dice1.top, &dice1.bottom, &dice1.right_side, &dice1.left_side);
				}
				else{
					moving_north(&dice1.top, &dice1.bottom, &dice1.front, &dice1.back);
				}
			}
		}

		//結果の出力
		printf("%d\n", dice1.right_side);
	}
	return 0;
}

//北側に転がす関数
void moving_north(int *top, int *bottom, int *front, int *back)
{
	int temp;

	//各面の値をずらす
	temp = *top;
	*top = *front;
	*front = *bottom;
	*bottom = *back;
	*back = temp;
}

//南側に転がす関数
void moving_south(int *top, int *bottom, int *front, int *back)
{
	int temp;

	//各面の値をずらす
	temp = *top;
	*top = *back;
	*back = *bottom;
	*bottom = *front;
	*front = temp;
}

//東側に転がす関数
void moving_east(int *top, int *bottom, int *right_side, int *left_side)
{
	int temp;

	//各面の値をずらす
	temp = *top;
	*top = *left_side;
	*left_side = *bottom;
	*bottom = *right_side;
	*right_side = temp;
}

//西側に転がす関数
void moving_west(int *top, int *bottom, int *right_side, int *left_side)
{
	int temp;

	//各面の値をずらす
	temp = *top;
	*top = *right_side;
	*right_side = *bottom;
	*bottom = *left_side;
	*left_side = temp;
}