#include <stdio.h>
#include <vector>
#include <array>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>

using std::string;
using std::vector;

class Dice {
private:
    // [ ][5][ ]
    // [4][1][3] 
    // [ ][2][ ] 
    int board[3][3];
    int bottom;
public:
    Dice(vector<int> legends) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = -1;
            }
        }
        this->board[0][1] = legends[5-1];
        this->board[1][0] = legends[4-1];
        this->board[1][1] = legends[1-1];
        this->board[1][2] = legends[3-1];
        this->board[2][1] = legends[2-1];
        this->bottom = legends[6-1]; 
        // printBoard();
    }
    void move(char direction) {
        switch (direction) {
            case 'N': {
                int tempBottom = board[0][1];
                board[0][1]    = board[1][1];
                board[1][1]    = board[2][1];
                board[2][1]    = this->bottom;
                this->bottom   = tempBottom;
                break;
            }

            case 'E': {
                int tempBottom = board[1][2];
                board[1][2]    = board[1][1];
                board[1][1]    = board[1][0];
                board[1][0]    = this->bottom;
                this->bottom   = tempBottom;
                break;
            }
            case 'W': {
                int tempBottom = board[1][0];
                board[1][0]    = board[1][1];
                board[1][1]    = board[1][2];
                board[1][2]    = this->bottom;
                this->bottom   = tempBottom;
                break;
            }
            case 'S': {
                int tempBottom = board[2][1];
                board[2][1]    = board[1][1];
                board[1][1]    = board[0][1];
                board[0][1]    = this->bottom;
                this->bottom   = tempBottom;
                break;
            }
        }
    }
    void printBoard() {
        // printf("------------\n");
        // for(int i = 0; i < 3; i++) {
        //     for(int j = 0; j < 3; j++){
        //         printf("[% d] ", this->board[i][j]);
        //     }
        //     printf("\n");
        // }
    }    
    int top() {
        return this->board[1][1];
    }
    int front() {
        return this->board[2][1];
    }
    void front(int n){
        this->board[2][1] = n;
    }
    int left() {
        return this->board[1][0];
    }
    void left(int n){
        this->board[1][0] = n;
    }
    int right() {
        return this->board[1][2];
    }
    void right(int n) {
        this->board[1][2] = n;
    }
    int back() {
        return this->board[0][1];
    }
    void back(int n) {
        this->board[0][1] = n;
    }
    void clockwise() {
        int temp = this->back();
        this->back(this->left());
        this->left(this->front());
        this->front(this->right());
        this->right(temp);
        printBoard();
    }
    int getRight(int front) {
        for (int i = 0; i < 4; i++){
            this->clockwise();
            if (this->front() == front) return this->right();
        }
        return -2;
    }

    int searchRight(int top, int front) {
        for (int i = 0; i < 6; i++) {
            const char direction = i % 2 == 0 ? 'S' : 'W';
            this->move(direction);
            if (top == this->top()) {
                return this->getRight(front);
            } 
        }
        return -1;
    }
};
int main(){
    int n1, n2, n3, n4, n5, n6;
    scanf("%d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6);
    Dice dice({ n1, n2, n3, n4, n5, n6 });

    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        int top, front;
        scanf("%d %d", &top, &front);
        printf("%d\n", dice.searchRight(top, front));
    }
    
    return 0;
}


Measured memory_bytes: 3208