#include <stdio.h>
#include <string.h>

typedef struct
{
        int top;
        int south_side;
        int east_side;
        int west_side;
        int north_side;
        int bottom;
} Dice;

Dice operation_right_rotation_from_topside( Dice dice_tmp )
{
        int tmp = dice_tmp.south_side;
        dice_tmp.south_side = dice_tmp.east_side;
        dice_tmp.east_side = dice_tmp.north_side;
        dice_tmp.north_side = dice_tmp.west_side;
        dice_tmp.west_side = tmp;
        return dice_tmp;
}

Dice change_top_to_northside( Dice dice_tmp )
{
        int tmp = dice_tmp.top;
        dice_tmp.top = dice_tmp.north_side;
        dice_tmp.north_side = dice_tmp.bottom;
        dice_tmp.bottom =  dice_tmp.south_side;
        dice_tmp.south_side = tmp;
        return dice_tmp;
}

Dice change_top_to_southside( Dice dice_tmp )
{
        int tmp = dice_tmp.top;
        dice_tmp.top = dice_tmp.south_side;
        dice_tmp.south_side = dice_tmp.bottom;
        dice_tmp.bottom = dice_tmp.north_side;
        dice_tmp.north_side = tmp;
        return dice_tmp;
}

Dice change_top_to_westside( Dice dice_tmp )
{
        int tmp = dice_tmp.top;
        dice_tmp.top = dice_tmp.west_side;
        dice_tmp.west_side = dice_tmp.bottom;
        dice_tmp.bottom = dice_tmp.east_side;
        dice_tmp.east_side = tmp;
        return dice_tmp;
}

Dice change_top_to_eastside( Dice dice_tmp )
{
        int tmp = dice_tmp.top;
        dice_tmp.top = dice_tmp.east_side;
        dice_tmp.east_side = dice_tmp.bottom;
        dice_tmp.bottom = dice_tmp.west_side;
        dice_tmp.west_side = tmp;
        return dice_tmp;
}

Dice change_top_to_bottom( Dice dice_tmp )
{
        int tmp = dice_tmp.top;
        dice_tmp.top = dice_tmp.bottom;
        dice_tmp.bottom = tmp;

        tmp = dice_tmp.west_side;
        dice_tmp.west_side = dice_tmp.east_side;
        dice_tmp.east_side = tmp;
        return dice_tmp;
}

int main()
{
        Dice dice;
        int topnum = 0, southnum = 0, east[100];
        int operation_number = 0, count = 0, tmp;

        scanf("%d %d %d %d %d %d", &dice.top, &dice.south_side,
                        &dice.east_side, &dice.west_side, &dice.north_side, &dice.bottom);

        scanf("%d", &operation_number);

        while ( count < operation_number )
        {
                scanf("%d %d", &topnum, &southnum);
                if ( topnum == dice.bottom )
                {
                        dice = change_top_to_bottom( dice );
                }
                else if ( topnum == dice.north_side )
                {
                        dice = change_top_to_northside( dice );
                }
                else if ( topnum == dice.south_side )
                {
                        dice = change_top_to_southside( dice );
                }
                else if ( topnum == dice.west_side )
                {
                        dice = change_top_to_westside( dice );
                }
                else if ( topnum == dice.east_side )
                {
                        dice = change_top_to_eastside( dice );
                }
        //      printf("top:%d north:%d south:%d west:%d east:%d bottom:%d\n",
        //                      dice.top, dice.north_side, dice.south_side,
        //                      dice.west_side, dice.east_side, dice.bottom );
                while ( southnum != dice.south_side )
                {
                        dice = operation_right_rotation_from_topside( dice );
                        //printf("operation\n");
                }
                east[count] = dice.east_side;
                count = count + 1;
        }

        count = 0;
        while ( count < operation_number )
        {
                printf("%d\n", east[count]);
                count = count + 1;
        }
        return 0;
}

Measured memory_bytes: 596