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.util.*;
import java.util.stream.*;
class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] num = new int[6];
        for (int i = 0; i < 6; i++){
            num[i] = input.nextInt();
        }
        Dice dice = new Dice(num);
        int q,top,front;
        int a = 0;
        int b = 0;
        q = input.nextInt();
        for (int i = 0; i < q; i++){
            top = input.nextInt();
            front = input.nextInt();
            for (int j = 0; j < 6; j++){
                if (num[j] == top){
                    a = j;
                } else if (num[j] == front){
                    b = j;
                }
            }
            if ((a == 3 && b == 1) || (a == 1 && b == 2) || (a == 2 && b == 4) || (a == 4 && b == 3)){
                dice.disp_num(0);
            } else if ((a == 2 && b == 0) || (a == 5 && b == 2) || (a == 3 && b == 5) || (a == 0 && b == 3)){
                dice.disp_num(1);
            } else if ((a == 0 && b == 1) || (a == 1 && b == 5) || (a == 5 && b == 4) || (a == 4 && b == 0)){
                dice.disp_num(2);
            } else if ((a == 1 && b == 0) || (a == 5 && b == 1) || (a == 4 && b == 5) || (a == 0 && b == 4)){
                dice.disp_num(3);
            } else if ((a == 0 && b == 2) || (a == 2 && b == 5) || (a == 5 && b == 3) || (a == 3 && b == 0)){
                dice.disp_num(4);
            } else{
                dice.disp_num(5);
            }
        }
    }
}

class Dice {
    private int N,E,W,S,T,B;
    private int[] dice_num;
    Dice(int[] num_array){
        dice_num = num_array;
        T = dice_num[0];
        S = dice_num[1];
        E = dice_num[2];
        W = dice_num[3];
        N = dice_num[4];
        B = dice_num[5];
    }
    public void roll(char direction){
        int tmp;
        if (direction == 'N'){
            tmp = T;
            T = S;
            S = B;
            B = N;
            N = tmp;
        } else if (direction == 'E'){
            tmp = T;
            T = W;
            W = B;
            B = E;
            E = tmp;
        } else if (direction == 'W'){
            tmp = T;
            T = E;
            E = B;
            B = W;
            W = tmp;
        } else {
            tmp = T;
            T = N;
            N = B;
            B = S;
            S = tmp;
        }
    }
    public void disp_top(){
        System.out.println(T);
    }
    public void disp_num(int n){
        System.out.println(dice_num[n]);
    }
}