#include <stdio.h>

#define ZERO 0																/*0???????????????*/
#define ONE 1																/*1???????????????*/
#define TWO 2																/*2???????????????*/
#define THREE 3																/*3???????????????*/
#define FOUR 4																/*4???????????????*/
#define FIVE 5																/*5???????????????*/
#define DICE_SURF 6															/*?????????????????¢??°*/

typedef struct {
	unsigned short usG_Dsurf[ DICE_SURF ];									/*??¢?????°?????????*/
	unsigned short usG_Dtop;												/*?????¢?????°???*/
	unsigned short usG_Dtop_add;											/*?????¢???????????????*/
	unsigned short usG_Dfront;												/*?????¢?????°???*/
	unsigned short usG_Dfront_add;											/*?????¢???????????????*/
} dice;

int main( void ){

	dice dice = { ZERO, ZERO, ZERO, ZERO, ZERO };						/*dice1????????????*/

	unsigned short usW_Dq_number;											/*???????????°*/
	unsigned short usW_Ci;													/*????????????i*/
	unsigned short usW_Cj;													/*????????????j*/
	
	/*??¢?????°???????????????*/
	for( usW_Ci = ZERO; usW_Ci < DICE_SURF; usW_Ci++){
		scanf( "%d", &dice.usG_Dsurf[ usW_Ci ] );						/*?????????????????°???????????????*/
	}
	scanf( "%d", &usW_Dq_number );											/*???????????°????????????*/
	/*???????????°???????????????*/
	for( usW_Ci = ZERO; usW_Ci < usW_Dq_number; usW_Ci++ ){
		scanf( "%d", &dice.usG_Dtop );									/*?????¢?????°???????????????*/
		/*??¢?????°???????????????*/
		for( usW_Cj = ZERO; usW_Cj < DICE_SURF; usW_Cj++ ){
			/*?????¢?????°?????¨?????????????????¢????????´????????´???*/
			if( dice.usG_Dtop == dice.usG_Dsurf[ usW_Cj ] ){
				dice.usG_Dtop_add = usW_Cj;								/*?????¢????????????????????????*/
				break;
			}
			else{
			/*else????????????????????????????????\*/
			}
		}
		scanf( "%d", &dice.usG_Dfront );									/*?????¢?????°???????????????*/
		/*??¢?????°???????????????*/
		for( usW_Cj = ZERO; usW_Cj < DICE_SURF; usW_Cj++ ){
			/*??£??¢?????°?????¨?????????????????¢?????°???????????´????????´???*/
			if( dice.usG_Dfront == dice.usG_Dsurf[ usW_Cj ] ){
				dice.usG_Dfront_add = usW_Cj;							/*?????¢????????????????????????*/
				break;
			}
			else{
			/*else????????????????????????????????\*/
			}
		}
		
		/*?????¢??????????????????????????????*/
		switch(dice.usG_Dtop_add){
			case ZERO:														/*1?????¢?????´???*/
				/*?????¢??????????????????????????????*/
				switch( dice.usG_Dfront_add ){
					case ONE:												/*2?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ TWO ] );		/*3?????¢?????????*/
						break;
					case TWO:												/*3?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FOUR ] );		/*5?????¢?????????*/
						break;
					case THREE:												/*4?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ONE ] );		/*2?????¢?????????*/
						break;
					case FOUR:												/*5?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ THREE ] );		/*4?????¢?????????*/
						break;
					default:
						/*default????????????????????????????????\*/
						break;
				}
				break;
			case ONE:														/*2?????¢?????´???*/
				/*?????¢??????????????????????????????*/
				switch( dice.usG_Dfront_add ){
					case ZERO:												/*1?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ THREE ] );		/*4?????¢?????????*/
						break;
					case TWO:												/*3?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ZERO ] );		/*1?????¢?????????*/
						break;
					case THREE:												/*4?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FIVE ] );		/*6?????¢?????????*/
						break;
					case FIVE:												/*6?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ TWO ] );		/*3?????¢?????????*/
						break;
					default:
						/*default????????????????????????????????\*/
						break;
				}
				break;
			case TWO:														/*3?????¢?????´???*/
				/*?????¢??????????????????????????????*/
				switch( dice.usG_Dfront_add ){
					case ZERO:												/*1?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ONE ] );		/*2?????¢?????????*/
						break;
					case ONE:												/*2?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FIVE ] );		/*6?????¢?????????*/
						break;
					case FOUR:												/*5?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ZERO ] );		/*1?????¢?????????*/
						break;
					case FIVE:												/*6?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FOUR ] );		/*5?????¢?????????*/
						break;
					default:
						/*default????????????????????????????????\*/
						break;
				}
				break;
			case THREE:														/*4?????¢?????´???*/
				/*?????¢??????????????????????????????*/
				switch( dice.usG_Dfront_add ){
					case ZERO:												/*1?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FOUR ] );		/*5?????¢?????????*/
						break;
					case ONE:												/*2?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ZERO ] );		/*1?????¢?????????*/
						break;
					case FOUR:												/*5?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FIVE ] );		/*6?????¢?????????*/
						break;
					case FIVE:												/*6?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ONE ] );		/*1?????¢?????????*/
						break;
					default:
						/*default????????????????????????????????\*/
						break;
				}
				break;
			case FOUR:														/*5?????¢?????´???*/
				/*?????¢??????????????????????????????*/
				switch( dice.usG_Dfront_add ){
					case ZERO:												/*1?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ TWO ] );		/*3?????¢?????????*/
						break;
					case TWO:												/*3?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FIVE ] );		/*6?????¢?????????*/
						break;
					case THREE:												/*4?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ZERO ] );		/*1?????¢?????????*/
						break;
					case FIVE:												/*6?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ THREE ] );		/*4?????¢?????????*/
						break;
					default:
						/*default????????????????????????????????\*/
						break;
				}
				break;
			case FIVE:														/*6?????¢?????´???*/
				/*?????¢??????????????????????????????*/
				switch(dice.usG_Dfront_add){
					case ONE:												/*2?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ THREE ] );		/*4?????¢?????????*/
						break;
					case TWO:												/*3?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ ONE ] );		/*2?????¢?????????*/
						break;
					case THREE:												/*4?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ FOUR ] );		/*5?????¢?????????*/
						break;
					case FOUR:												/*5?????¢?????´???*/
						printf( "%d\n", dice.usG_Dsurf[ TWO ] );		/*3?????¢?????????*/
						break;
					default:
						/*default????????????????????????????????\*/
						break;
				}
				break;
			default:
				/*default????????????????????????????????\*/
				break;
		}/*?????¢?????????????????????????????????????????§*/
	}/*???????????°??????????????????????????§*/

	return ZERO;
}

Measured memory_bytes: 596