#include <iostream>


class Dice {
    public:
        int* label;
        Dice() {
            label = new int[7];
        };
        ~Dice() {
            delete[] label;
        }
        void turn_dice(char c) {
           
            if(c == 'S') {
               int w = label[5];
               label[5] = label[6];
               label[6] = label[2];
               label[2] = label[1];
               label[1] = w;
            }
            else if(c == 'E') {
                int w = label[4];
                label[4] = label[6];
                label[6] = label[3];
                label[3] = label[1];
                label[1] = w;
            }
            else if(c == 'N') {
                int w = label[2];
                label[2] = label[6];
                label[6] = label[5];
                label[5] = label[1];
                label[1] = w;
            }
            else if(c == 'W') {
                int w = label[3];
                label[3] = label[6];
                label[6] = label[4];
                label[4] = label[1];
                label[1] = w;
            }
        };
        int right(int top,int front) {
            if( (top == label[5] && front == label[4])||
                (top == label[4] && front == label[2])||
                (top == label[2] && front == label[3])||
                (top == label[3] && front == label[5]) ) 
            {
            return label[1];
            }
            if( (top == label[4] && front == label[5])||
                (top == label[5] && front == label[3])||
                (top == label[3] && front == label[2])||
                (top == label[2] && front == label[4]) ) 
            {
            return label[6];
            }
            if( (top == label[1] && front == label[4])||
                (top == label[4] && front == label[6])||
                (top == label[6] && front == label[3])||
                (top == label[3] && front == label[1]) ) 
            {
            return label[2];
            }
            if( (top == label[4] && front == label[1])||
                (top == label[1] && front == label[3])||
                (top == label[3] && front == label[6])||
                (top == label[6] && front == label[4]) ) 
            {
            return label[5];
            }
            if( (top == label[5] && front == label[1])||
                (top == label[1] && front == label[2])||
                (top == label[2] && front == label[6])||
                (top == label[6] && front == label[5]) ) 
            {
            return label[3];
            }
            if( (top == label[5] && front == label[6])||
                (top == label[6] && front == label[2])||
                (top == label[2] && front == label[1])||
                (top == label[1] && front == label[5]) ) 
            {
            return label[4];
            }
        };
};


int main() {
    int q;
    int a[26],b[26];
    Dice dice1;
    for(int i = 1; i <= 6; i++) {
       std::cin >> dice1.label[i];
    }
    std::cin >> q;
    for(int j = 1; j <= q; j++) {
        std::cin >> a[j];
        std::cin >> b[j];
    }
    for(int k = 1;k <= q; k++) {
        std::cout << dice1.right(a[k],b[k]) << std::endl;
    }

    return 0;
}


Measured memory_bytes: 3072