Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

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.

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.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

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

Sample Output

3
5
6

Solution in JAVA (judge verdict: Accepted)

import java.io.*;

class Dice{
    int[] sides = new int[6];
    int j = 0;

    Dice(int[] sides){
        this.sides = sides;
    }

    void N(){
        int tmp = sides[0];
        sides[0] = sides[1];
        sides[1] = sides[5];
        sides[5] = sides[4];
        sides[4] = tmp;
    }

    void S(){
        int tmp = sides[0];
        sides[0] = sides[4];
        sides[4] = sides[5];
        sides[5] = sides[1];
        sides[1] = tmp;
    }

    void W(){
        int tmp = sides[0];
        sides[0] = sides[2];
        sides[2] = sides[5];
        sides[5] = sides[3];
        sides[3] = tmp;
    }

    void E(){
        int tmp = sides[0];
        sides[0] = sides[3];
        sides[3] = sides[5];
        sides[5] = sides[2];
        sides[2] = tmp;
    }

    int getSide(int i){
        return sides[i];
    }
}

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String[] str = br.readLine().split(" ");
        int[] sides = new int[6];
        int n = Integer.parseInt(br.readLine());

        for(int i = 0; i < 6; i++){
            sides[i] = Integer.parseInt(str[i]);
        }
        Dice dice = new Dice(sides);

        for(int i = 0; i < n; i++){
            String[] line = br.readLine().split(" ");
            int upper = Integer.parseInt(line[0]);
            int front = Integer.parseInt(line[1]);
            int cnt = 0;
            while(dice.getSide(0) != upper){
                if(cnt % 4 == 3){
                    dice.E();
                }else{
                    dice.N();
                }
                cnt++;
            }
            dice.W();
            while(dice.getSide(0) != front){
                dice.N();
            }
            sb.append(dice.getSide(4)).append("\n");
        }
        System.out.print(sb.toString());
    }
}