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>

struct DICE {
int d1,d2,d3,d4,d5,d6;
};

void MoveDice(struct DICE *p,char c){
    int tmp;
    tmp=p->d1;
    switch(c){
        case 'W':
            p->d1=p->d3;p->d3=p->d6;p->d6=p->d4;p->d4=tmp;
            break;
        case 'E':
            p->d1=p->d4;p->d4=p->d6;p->d6=p->d3;p->d3=tmp;
            break;
        case 'N':
            p->d1=p->d2;p->d2=p->d6;p->d6=p->d5;p->d5=tmp;
            break;
        case 'S':
            p->d1=p->d5;p->d5=p->d6;p->d6=p->d2;p->d2=tmp;
            break;
    }
}
int main(){
    struct DICE p;
    int q;
    int n1,n2;
    scanf("%d %d %d %d %d %d\n",&p.d1,&p.d2,&p.d3,&p.d4,&p.d5,&p.d6);
    scanf("%d", &q);
    while(q--){
        scanf("%d %d", &n1,&n2);
        if(n2 == 4){
            MoveDice(&p,'E');
            MoveDice(&p,'S');
            while(n1 != p.d1){
                MoveDice(&p,'W');
            }
        }
        else if(n2 == 3){
            MoveDice(&p,'W');
            MoveDice(&p,'S');
            while(n1 != p.d1){
                MoveDice(&p,'W');
            }
        }
        else if(n2 == 5){
            MoveDice(&p,'S');
            MoveDice(&p,'S');
            while(n1 != p.d1){
                MoveDice(&p,'W');
            }
        }
        else if(n2 == 1){
            MoveDice(&p,'S');
            while(n1 != p.d1){
                MoveDice(&p,'W');
            }
        }
        else if(n2 == 6){
            MoveDice(&p,'N');
            while(n1 != p.d1){
                MoveDice(&p,'W');
            }
        }
        else{
            while(n1 != p.d1){
                MoveDice(&p,'W');
            }
        }
        printf("%d\n",p.d3);
    }
    return 0;
}