#include<iostream>
#include<cstdio>

using namespace std;

class Cube{
    public:
    Cube(){}
    int f[6];
    
    void roll_x(){
        roll(0, 1, 5, 4);
    }
    void roll_y(){
        roll(0, 2, 5, 3);
    }
    void roll_z(){
        roll(1, 2, 4, 3);
    }
    
    void roll(int i, int j, int k, int l){
        int t = f[i]; f[i] = f[j]; f[j] = f[k]; f[k] = f[l]; f[l] = t;
    }
};

int getRight(Cube c1, int f1, int f2){
    for(int i = 0; i < 6; i++){
        for(int j = 0; j < 4; j++){
            if(c1.f[0] == f1 && c1.f[1] == f2) return c1.f[2];
            c1.roll_z();
        }
        if(i % 2 == 0) c1.roll_y();
        else c1.roll_x();
    }
    return -1;
}

int main(){
    Cube c;
    int f1, f2;
    
    for(int i = 0; i < 6; i++){
        cin >> c.f[i];
    }
    int q;
    cin >> q;
    
    for(int i = 0; i < q; i++){
        cin >> f1 >> f2;
        cout << getRight(c, f1, f2) << endl;
    }
    
    return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.


<image>


Constraints

* $0 \leq $ the integer assigned to a face $ \leq 100$
* The integers are all different
* $1 \leq q \leq 24$

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels. In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Example

Input

1 2 3 4 5 6
3
6 5
1 3
3 2


Output

3
5
6

### Response

```cpp
#include<iostream>
#include<cstdio>

using namespace std;

class Cube{
    public:
    Cube(){}
    int f[6];
    
    void roll_x(){
        roll(0, 1, 5, 4);
    }
    void roll_y(){
        roll(0, 2, 5, 3);
    }
    void roll_z(){
        roll(1, 2, 4, 3);
    }
    
    void roll(int i, int j, int k, int l){
        int t = f[i]; f[i] = f[j]; f[j] = f[k]; f[k] = f[l]; f[l] = t;
    }
};

int getRight(Cube c1, int f1, int f2){
    for(int i = 0; i < 6; i++){
        for(int j = 0; j < 4; j++){
            if(c1.f[0] == f1 && c1.f[1] == f2) return c1.f[2];
            c1.roll_z();
        }
        if(i % 2 == 0) c1.roll_y();
        else c1.roll_x();
    }
    return -1;
}

int main(){
    Cube c;
    int f1, f2;
    
    for(int i = 0; i < 6; i++){
        cin >> c.f[i];
    }
    int q;
    cin >> q;
    
    for(int i = 0; i < q; i++){
        cin >> f1 >> f2;
        cout << getRight(c, f1, f2) << endl;
    }
    
    return 0;
}

```