#include<stdio.h>

#define MAX_DIR_SIZE      (101)
#define MAX_SURFACE_COUNT (6)
#define MAX_DATASET_COUNT (24)

typedef union _dice{
        int number[MAX_SURFACE_COUNT];
        struct {
                int front;
                int south;
                int east;
                int west;
                int north;
                int back;
        } pos;
} DICE;

void roll_dice(DICE * dice, char direction);
int  get_east_surface(const DICE * dice, int front, int south);

int main(void)
{
        DICE dice;
        int index, dataset_count;
        int front, south;
        int easts[MAX_DATASET_COUNT];

        for(index=0; index < MAX_SURFACE_COUNT; index++){
                scanf("%d", &dice.number[index]);
        }

        scanf("%d", &dataset_count);

        for(index = 0; index < dataset_count; index++){
                scanf("%d%d", &front, &south);

                easts[index] = get_east_surface(&dice, front, south);
        }

        for(index = 0; index < dataset_count; index++){
                printf("%d\n", easts[index]);
        }

        return 0;
}

void roll_dice(DICE * dice, char direction){

        int index;
        DICE d;

        switch(direction){
        case 'N':
                d.pos.front = dice->pos.south;
                d.pos.south = dice->pos.back;
                d.pos.east  = dice->pos.east;
                d.pos.west  = dice->pos.west;
                d.pos.north = dice->pos.front;
                d.pos.back  = dice->pos.north;
                break;
        case 'S':
                d.pos.front = dice->pos.north;
                d.pos.south = dice->pos.front;
                d.pos.east  = dice->pos.east;
                d.pos.west  = dice->pos.west;
                d.pos.north = dice->pos.back;
                d.pos.back  = dice->pos.south;
                break;
        case 'W':
                d.pos.front = dice->pos.east;
                d.pos.south = dice->pos.south;
                d.pos.east  = dice->pos.back;
                d.pos.west  = dice->pos.front;
                d.pos.north = dice->pos.north;
                d.pos.back  = dice->pos.west;
                break;
        case 'E':
                d.pos.front = dice->pos.west;
                d.pos.south = dice->pos.south;
                d.pos.east  = dice->pos.front;
                d.pos.west  = dice->pos.back;
                d.pos.north = dice->pos.north;
                d.pos.back  = dice->pos.east;
                break;
        case 'R':
                d.pos.front = dice->pos.front;
                d.pos.south = dice->pos.east;
                d.pos.east  = dice->pos.north;
                d.pos.west  = dice->pos.south;
                d.pos.north = dice->pos.west;
                d.pos.back  = dice->pos.back;
                break;
        case 'L':
                d.pos.front = dice->pos.front;
                d.pos.south = dice->pos.west;
                d.pos.east  = dice->pos.south;
                d.pos.west  = dice->pos.north;
                d.pos.north = dice->pos.east;
                d.pos.back  = dice->pos.back;
                break;
        default:
                break;
        }

        for(index=0; index < MAX_SURFACE_COUNT; index++){
                dice->number[index] = d.number[index];
        }

}

int  get_east_surface(const DICE * dice, int front, int south)
{
        DICE search_dice = *dice;

        if(search_dice.pos.front != front){
        int before_front = search_dice.pos.front;

                do{
                        roll_dice(&search_dice, 'N');

                        if(search_dice.pos.front == front){
                                break;
                        }

                }while(search_dice.pos.front != before_front);

                if(search_dice.pos.front != front){
                        do{
                                roll_dice(&search_dice, 'W');

                                if(search_dice.pos.front == front){
                                        break;
                                }

                        }while(search_dice.pos.front != before_front);
                }
        }


        if(search_dice.pos.south != south){
        int before_south = search_dice.pos.south;

                do{
                        roll_dice(&search_dice, 'R');

                        if(search_dice.pos.south == south){
                                break;
                        }

                }while(search_dice.pos.south != before_south);
        }

        return search_dice.pos.east;
}


Measured memory_bytes: 2052