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.PrintWriter;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter pw = new PrintWriter(System.out);

    int[] spots = new int[6];
    for (int i = 0; i < 6; i++) {
        spots[i] = sc.nextInt();
    }

    Dice dice = new Main().new Dice(spots);

    int q = sc.nextInt();
    for (int i = 0; i < q; i++) {
        int topN = sc.nextInt();
        int frontN = sc.nextInt();
        dice = dice.topOf(topN);
        dice = dice.frontOf(frontN);
        pw.println(dice.right());
    }

    pw.flush();
  }

  class Dice {

    private int[] spots = new int[6];

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

    public Dice north() {
        return new Dice(new int[]{
            front(),
            bottom(),
            right(),
            left(),
            top(),
            back()
        });
    }

    public Dice west() {
        return new Dice(new int[]{
            right(),
            front(),
            bottom(),
            top(),
            back(),
            left()
        });
    }

    public Dice east() {
        return new Dice(new int[]{
            left(),
            front(),
            top(),
            bottom(),
            back(),
            right()
        });

    }

    public Dice south() {
        return new Dice(new int[]{
            back(),
            top(),
            right(),
            left(),
            bottom(),
            front()
        });
    }

    public Dice rotate() {
        return new Dice(new int[]{
            top(),
            left(),
            front(),
            back(),
            right(),
            bottom()
        });
    }

    public int top() {
        return spots[0];
    }

    public int bottom() {
        return spots[5];
    }

    public int front() {
        return spots[1];
    }

    public int back() {
        return spots[4];
    }

    public int left() {
        return spots[3];
    }

    public int right() {
        return spots[2];
    }

    public Dice topOf(int topN) {
        Dice[] dices = new Dice[]{
            this,
            this.south(),
            this.east(),
            this.west(),
            this.north(),
            this.south().south()
        };
        for (Dice dice : dices) {
            if (dice.top() == topN) {
                return dice;
            }
        }
        return null;
    }

    public Dice frontOf(int frontN) {
        Dice[] dices = new Dice[]{
            this.rotate(),
            this.rotate().rotate(),
            this.rotate().rotate().rotate(),
            this.rotate().rotate().rotate().rotate(),
        };
        for (Dice dice : dices) {
            if (dice.front() == frontN) {
                return dice;
            }
        }
        return null;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < spots.length; i++) {
            sb.append(i + ": " + spots[i] + ", ");
        }
        sb.append("]");
        return sb.toString();
    }
  }
}