#include <iostream>
#include <string>
#define MAXQ 24
using namespace std;

class DICE {
    int top, front, right, left, back, bottom;
public:
    void setValue (int *diceValue);
    void rotate (char order);
    int showValue (int direction);
};

void DICE::setValue (int *diceValue) {
    DICE::top    = diceValue[0];
    DICE::front  = diceValue[1];
    DICE::right  = diceValue[2];
    DICE::left   = diceValue[3];
    DICE::back   = diceValue[4];
    DICE::bottom = diceValue[5];
}

void DICE::rotate (char order) {
    int tmp;
    switch (order) {
        case 'E':
            tmp = DICE::right;
            DICE::right  = DICE::top;
            DICE::top    = DICE::left;
            DICE::left   = DICE::bottom;
            DICE::bottom = tmp;
            break;
        case 'N':
            tmp = DICE::back;
            DICE::back   = DICE::top;
            DICE::top    = DICE::front;
            DICE::front  = DICE::bottom;
            DICE::bottom = tmp;
            break;
        case 'S':
            tmp = DICE::front;
            DICE::front  = DICE::top;
            DICE::top    = DICE::back;
            DICE::back   = DICE::bottom;
            DICE::bottom = tmp;
            break;
        case 'W':
            tmp = DICE::left;
            DICE::left   = DICE::top;
            DICE::top    = DICE::right;
            DICE::right  = DICE::bottom;
            DICE::bottom = tmp;
            break;
        case 'R': // for rotation right to left
            tmp = DICE::left;
            DICE::left  = DICE::front;
            DICE::front = DICE::right;
            DICE::right = DICE::back;
            DICE::back  = tmp;
            break;
        default:
            break;
    }
}

int DICE::showValue (int direction) {
    int value;
    switch (direction) {
        case 0:
            value = DICE::top;
            break;
        case 1:
            value = DICE::front;
            break;
        case 2:
            value = DICE::right;
            break;
        case 3:
            value = DICE::left;
            break;
        case 4:
            value = DICE::back;
            break;
        case 5:
            value = DICE::bottom;
            break;
        default:
            break;
    }
    return value;
}

int solve (int *diceValue, int top, int front) {
    DICE dice;
    int result = 0;
    dice.setValue (diceValue);
    for (int e=0; e<4; e++) {
        for (int n=0; n<4; n++) {
            for (int r=0; r<4; r++) {
                if ( dice.showValue(0) == top && dice.showValue(1) == front ) {
                    result = dice.showValue(2);
                    return result;
                }
                dice.rotate('R');
            }
            dice.rotate('N');
        }
        dice.rotate('E');
    }
    return result = -1;
}

int main () {
    // input
    int diceValue[6];
    for (int i=0; i<6; i++) {
        cin >> diceValue[i];
    }

    int qnum; // number of questions
    cin >> qnum;
    
    int top[MAXQ], front[MAXQ];
    for (int i=0; i<qnum; i++) {
        cin >> top[i] >> front[i];
    }

    // solve
    int result[MAXQ];
    for (int i=0; i<qnum; i++) {
        result[i] = solve(diceValue, top[i], front[i]);
    }

    // output
    for (int i=0; i<qnum; i++) {
        cout << result[i] << endl;
    }
}


Measured memory_bytes: 3140