//============================================================================
//dice1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct{
    int N;
    int E;
    int W;
    int S;
    int T;
    int B;
}TCycoro;
enum {pos1, pos2, pos3, pos4, pos5, pos6, posEND};
char Cmd[101];
//----------------------------------------------------------------------------
void CyRot(TCycoro *cyc, char dir)
{
    int S;

    switch(dir){
        case 'N': // do not move EW
             S=cyc->N, cyc->N = cyc->T, cyc->T=cyc->S, cyc->S=cyc->B, cyc->B=S; break;
        case 'E': // do not move NS
             S=cyc->E, cyc->E = cyc->T, cyc->T=cyc->W, cyc->W=cyc->B, cyc->B=S; break;
        case 'W': // do not move NS
             S=cyc->W, cyc->W = cyc->T, cyc->T=cyc->E, cyc->E=cyc->B, cyc->B=S; break;
        case 'S': // do not move EW
             S=cyc->S, cyc->S = cyc->T, cyc->T=cyc->N, cyc->N=cyc->B, cyc->B=S; break;
    }
}
//----------------------------------------------------------------------------
void prndice(TCycoro *cyc, char dir)
{
    printf("dir:%c, N=%d, E=%d, W=%d, S=%d, T=%d, B=%d\n", dir,
            cyc->N+1, cyc->E+1, cyc->W+1, cyc->S+1, cyc->T+1, cyc->B+1);
}
//----------------------------------------------------------------------------
int rgtside(int t, int f)
{
    switch(t){
        case pos1:
            switch(f){
                case pos2: return pos3;
                case pos3: return pos5;
                case pos5: return pos4;
                case pos4: return pos2;
            }
            break;
        case pos2:
            switch(f){
                case pos1: return pos4;
                case pos3: return pos1;
                case pos4: return pos6;
                case pos6: return pos3;
            }
            break;
        case pos3:
            switch(f){
                case pos1: return pos2;
                case pos2: return pos6;
                case pos5: return pos1;
                case pos6: return pos5;
            }
            break;
        case pos4:
            switch(f){
                case pos1: return pos5;
                case pos2: return pos1;
                case pos5: return pos6;
                case pos6: return pos2;
            }
            break;
        case pos5:
            switch(f){
                case pos1: return pos3;
                case pos3: return pos6;
                case pos4: return pos1;
                case pos6: return pos4;
            }
            break;
        case pos6:
            switch(f){
                case pos2: return pos4;
                case pos3: return pos2;
                case pos4: return pos5;
                case pos5: return pos3;
            }
            break;
    }
    return -1;
}
//----------------------------------------------------------------------------
int main()
{
    int i,n,t,f,rgt, *top,*frn, cyval[posEND]; TCycoro Cyc; 

    Cyc.N=pos5;
    Cyc.E=pos3;
    Cyc.W=pos4;
    Cyc.S=pos2;
    Cyc.T=pos1;
    Cyc.B=pos6;
    for (i=0; i<posEND; i++)
        scanf("%d", &cyval[i]);
    scanf("%d",&n);
    top=malloc(n*sizeof(int));
    frn=malloc(n*sizeof(int));
    for (i=0; i<n; i++)
	    scanf("%d %d", &top[i], &frn[i]);
    //for (i=0; i<n; i++)
	//    printf("%d:%d %d\n", i, top[i], frn[i]);
    for (i=0; i<n; i++){
        for (t=0; cyval[t]!=top[i]; t++);
        for (f=0; cyval[f]!=frn[i]; f++);
        rgt=rgtside(t, f);
        //printf("t:%d, f:%d, %d\n", t, f, cyval[rgt]);
        printf("%d\n", cyval[rgt]);
    }
    free(top);
    free(frn);
    //printf("%d\n", rgt);
    return 0;
}
//----------------------------------------------------------------------------

Measured memory_bytes: 596