#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* common define */
/* TODO create common.h */
#define U1 unsigned char
#define S1 char
#define	S4 int
#define DU double
#define VD void

#define TRUE              (1)
#define FALSE             (0)

/* define */
#define	MAX_COMMAND_BYTES	(1)
#define MAX_COMMAND_COUNT	(100)
#define NUM_KIND_OF_COMMAND	(6)
#define NUM_FACE_OF_DICE	(6)

typedef struct st_dice
{
	S4 s4_top;
	S4 s4_north;
	S4 s4_east;
	S4 s4_west;
	S4 s4_south;
	S4 s4_bottom;
}ST_DICE;

typedef struct st_commands_string
{
	S4 s4_command_count;
	U1 u1_commands_string[MAX_COMMAND_COUNT][MAX_COMMAND_BYTES + 1];
}ST_COMMANDS_STRING;

typedef VD(*COMMNAD_FUNC_TYPE)(ST_DICE*);

typedef struct st_command
{
	U1 u1_command_string[MAX_COMMAND_BYTES + 1];
	COMMNAD_FUNC_TYPE st_command_func;
}ST_COMMAND;

static VD vd_s_move_dice_to_answer_question(ST_DICE*, S4, S4);
static VD vd_s_move_dice_multiply(ST_COMMANDS_STRING*, ST_DICE*);
static VD vd_s_move_dice(U1[MAX_COMMAND_BYTES + 1], ST_DICE*);
static VD vd_s_North(ST_DICE*);
static VD vd_s_East(ST_DICE*);
static VD vd_s_West(ST_DICE*);
static VD vd_s_South(ST_DICE*);
static VD vd_s_Right(ST_DICE*);
static VD vd_s_Left(ST_DICE*);

const ST_COMMAND st_c_command[NUM_KIND_OF_COMMAND] =
{
	{ "N", vd_s_North },
	{ "E", vd_s_East },
	{ "W", vd_s_West },
	{ "S", vd_s_South },
	{ "R", vd_s_Right },
	{ "L", vd_s_Left }
};

S4 main()
{
	ST_DICE st_t_dice;
	S4 s4_t_index_dice_number;
	S4 s4_t_dice_nubmers[NUM_FACE_OF_DICE + 1];
	S4 s4_t_number_of_question;
	S4 s4_t_question_count;
	S4 s4_t_question_dice_top;
	S4 s4_t_question_dice_front;
	U1 u1_tp_commands_string[MAX_COMMAND_COUNT + 1];
	ST_COMMANDS_STRING st_t_commands_string;

	/* Input the dice numbers. */
	for (s4_t_index_dice_number = (S4)1; s4_t_index_dice_number <= (S4)NUM_FACE_OF_DICE; s4_t_index_dice_number++)
	{
		(VD)scanf("%d ", &(s4_t_dice_nubmers[s4_t_index_dice_number]));
	}
	st_t_dice.s4_top = s4_t_dice_nubmers[1];
	st_t_dice.s4_south = s4_t_dice_nubmers[2];
	st_t_dice.s4_east = s4_t_dice_nubmers[3];
	st_t_dice.s4_west = s4_t_dice_nubmers[4];
	st_t_dice.s4_north = s4_t_dice_nubmers[5];
	st_t_dice.s4_bottom = s4_t_dice_nubmers[6];

	/* Input the number of questions */
	(VD)scanf("%d ", &s4_t_number_of_question);

	for (s4_t_question_count = (S4)0; s4_t_question_count < s4_t_number_of_question; s4_t_question_count++)
	{
		/* Input the top and front face for question */
		(VD)scanf("%d ", &s4_t_question_dice_top);
		(VD)scanf("%d ", &s4_t_question_dice_front);

		/* Move the dice to answer question */
		vd_s_move_dice_to_answer_question(&st_t_dice, s4_t_question_dice_top, s4_t_question_dice_front);

		/* Print the number of the right of the dice. */
		printf("%d\n", st_t_dice.s4_east);
	}
	return((S4)0);
}

static VD vd_s_move_dice_to_answer_question(ST_DICE* st_ap_dice, S4 s4_a_question_dice_top, S4 s4_a_question_dice_front)
{
	S4 s4_t_dice_index;

	/* Move the dice to meet top */
	s4_t_dice_index = (S4)0;
	while ((s4_t_dice_index < (S4)3) &&
		(st_ap_dice->s4_top != s4_a_question_dice_top))
	{
		vd_s_North(st_ap_dice);
		s4_t_dice_index++;
	}
	s4_t_dice_index = (S4)0;
	while ((s4_t_dice_index < (S4)3) &&
		(st_ap_dice->s4_top != s4_a_question_dice_top))
	{
		vd_s_West(st_ap_dice);
		s4_t_dice_index++;
	}

	/* Move the dice to meet front */
	s4_t_dice_index = (S4)0;
	while ((s4_t_dice_index < (S4)3) &&
		(st_ap_dice->s4_south != s4_a_question_dice_front))
	{
		vd_s_Right(st_ap_dice);
		s4_t_dice_index++;
	}

	return;
}

static VD vd_s_move_dice_multiply(ST_COMMANDS_STRING* st_ap_commands_string, ST_DICE* st_ap_dice)
{
	S4 s4_t_commandIndex;

	/* Move the dice */
	for (s4_t_commandIndex = (S4)0; s4_t_commandIndex < st_ap_commands_string->s4_command_count; s4_t_commandIndex++)
	{
		(VD)vd_s_move_dice(st_ap_commands_string->u1_commands_string[s4_t_commandIndex], st_ap_dice);
	}

	return;
}

static VD vd_s_move_dice(U1 u1_ap_command_string[MAX_COMMAND_BYTES + 1], ST_DICE* st_ap_dice)
{
	S4 s4_t_command_index;
	S4 s4_t_isMatch;

	/* Excecute command. */
	for (s4_t_command_index = (S4)0; s4_t_command_index < (S4)NUM_KIND_OF_COMMAND; s4_t_command_index++)
	{
		s4_t_isMatch = strcmp(u1_ap_command_string, st_c_command[s4_t_command_index].u1_command_string);
		if (s4_t_isMatch == (S4)0)
		{
			st_c_command[s4_t_command_index].st_command_func(st_ap_dice);
		}
	}
	return;
}

static VD vd_s_North(ST_DICE* st_ap_dice)
{
	S4 s4_t_temp;
	s4_t_temp = st_ap_dice->s4_north;
	st_ap_dice->s4_north = st_ap_dice->s4_top;
	st_ap_dice->s4_top = st_ap_dice->s4_south;
	st_ap_dice->s4_south = st_ap_dice->s4_bottom;
	st_ap_dice->s4_bottom = s4_t_temp;
}

static VD vd_s_East(ST_DICE* st_ap_dice)
{
	S4 s4_t_temp;
	s4_t_temp = st_ap_dice->s4_east;
	st_ap_dice->s4_east = st_ap_dice->s4_top;
	st_ap_dice->s4_top = st_ap_dice->s4_west;
	st_ap_dice->s4_west = st_ap_dice->s4_bottom;
	st_ap_dice->s4_bottom = s4_t_temp;
}

static VD vd_s_West(ST_DICE* st_ap_dice)
{
	S4 s4_t_temp;
	s4_t_temp = st_ap_dice->s4_west;
	st_ap_dice->s4_west = st_ap_dice->s4_top;
	st_ap_dice->s4_top = st_ap_dice->s4_east;
	st_ap_dice->s4_east = st_ap_dice->s4_bottom;
	st_ap_dice->s4_bottom = s4_t_temp;
}

static VD vd_s_South(ST_DICE* st_ap_dice)
{
	S4 s4_t_temp;
	s4_t_temp = st_ap_dice->s4_south;
	st_ap_dice->s4_south = st_ap_dice->s4_top;
	st_ap_dice->s4_top = st_ap_dice->s4_north;
	st_ap_dice->s4_north = st_ap_dice->s4_bottom;
	st_ap_dice->s4_bottom = s4_t_temp;

}

static VD vd_s_Right(ST_DICE* st_ap_dice)
{
	S4 s4_t_temp;
	s4_t_temp = st_ap_dice->s4_north;
	st_ap_dice->s4_north = st_ap_dice->s4_west;
	st_ap_dice->s4_west = st_ap_dice->s4_south;
	st_ap_dice->s4_south = st_ap_dice->s4_east;
	st_ap_dice->s4_east = s4_t_temp;
}

static VD vd_s_Left(ST_DICE* st_ap_dice)
{
	S4 s4_t_temp;
	s4_t_temp = st_ap_dice->s4_north;
	st_ap_dice->s4_north = st_ap_dice->s4_east;
	st_ap_dice->s4_east = st_ap_dice->s4_south;
	st_ap_dice->s4_south = st_ap_dice->s4_west;
	st_ap_dice->s4_west = s4_t_temp;
}



Measured memory_bytes: 2148