// https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/11/ITP1_11_C
/*
      1        N
    4 2 3 5  W   E
      6        S

    upper:1 lower:6 front:2 back:5 left:4 right:3
    
    upper->front->right->left->back->lower

    N: 1->5 2->1 6->2 5->6
    S: 1->2 2->6 6->5 5->1  
    E: 1->3 3->6 6->4 4->1
    W: 1->4 4->6 6->3 3->1 

    right 1: /2 3/3 5/5 4/4 2/
    right 2: /1 4/4 6/6 3/3 1/
    right 3: /1 2/2 6/6 5/5 1/
    right 4: /1 5/5 6/6 2/2 1/
    right 5: /1 3/3 6/6 4/4 1/
    right 6: /2 4/4 5/5 3/3 2/
*/

#include <iostream>
#include <cstring>
#include <vector>

using std::vector;

class dice
{

public:
    dice();
    void SetPips();
    int GetUpperPip() { return pip[0]; };
    int GetArbRightPip(const vector<vector<vector<int>>> &V, int upper, int front);
    void RollDice(char order);
    void PrintDice();
    bool operator==(const dice &d2) const { return !memcmp(pip, d2.pip, sizeof(int) * 6); };
    bool operator!=(const dice &d2) const { return memcmp(pip, d2.pip, sizeof(int) * 6); };
    friend std::ostream &operator<<(std::ostream &out, const dice &d);

private:
    int pip[6];
};

int main()
{
    const vector<vector<vector<int>>> V =
        {{{1, 2}, {2, 4}, {4, 3}, {3, 1}},
         {{0, 3}, {3, 5}, {5, 2}, {2, 0}},
         {{0, 1}, {1, 5}, {5, 4}, {4, 0}},
         {{0, 4}, {4, 5}, {5, 1}, {1, 0}},
         {{0, 2}, {2, 5}, {5, 3}, {3, 0}},
         {{1, 3}, {3, 4}, {4, 2}, {2, 1}}};

    int q, upper, lower;
    dice d;

    d.SetPips();
    std::cin >> q;
    for (int i = 0; i < q; i++)
    {
        std::cin >> upper >> lower;
        std::cout << d.GetArbRightPip(V, upper, lower) << std::endl;
    }

    return 0;
}

dice::dice() : pip{}
{
}

void dice::SetPips()
{
    for (int i = 0; i < 6; i++)
        std::cin >> pip[i];
}

int dice::GetArbRightPip(const vector<vector<vector<int>>> &V, int upper, int front)
{
    int match;

    bool tmp[2] = {0, 0};
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            tmp[0] = tmp[1] = 0;
            for (int k = 0; k < 2; k++)
            {
                if (k == 0 && pip[V.at(i).at(j).at(k)] == upper)
                    tmp[k] = true;
                else if (k == 1 && pip[V.at(i).at(j).at(k)] == front)
                    tmp[k] = true;

                if (tmp[0] == true && tmp[1] == true)
                {
                    match = i;
                    return pip[match];
                }
            }
        }
    }
    return -1;
}

void dice::RollDice(char order)
{
    int buff;
    switch (order)
    {
    case 'N':
    {
        buff = pip[0];
        pip[0] = pip[1];
        pip[1] = pip[5];
        pip[5] = pip[4];
        pip[4] = buff;
        break;
    }
    case 'S':
    {
        buff = pip[0];
        pip[0] = pip[4];
        pip[4] = pip[5];
        pip[5] = pip[1];
        pip[1] = buff;
        break;
    }
    case 'E':
    {
        buff = pip[0];
        pip[0] = pip[3];
        pip[3] = pip[5];
        pip[5] = pip[2];
        pip[2] = buff;
        break;
    }
    case 'W':
    {
        buff = pip[0];
        pip[0] = pip[2];
        pip[2] = pip[5];
        pip[5] = pip[3];
        pip[3] = buff;
        break;
    }
    }
}

void dice::PrintDice()
{
    for (auto x : pip)
    {
        std::cout << x << " ";
    }
    std::cout << std::endl;
}

std::ostream &operator<<(std::ostream &out, const dice &d)
{
    for (auto &x : d.pip)
    {
        out << x << " ";
    }
}


Measured memory_bytes: 3136