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 Kotlin (judge verdict: Accepted)

import java.util.*

data class Dice(val value: List<Int>) {
    var top = 1
    var south = 2
    var east = 3
    var west = 4
    var north = 5
    var bottom = 6

    fun rolls(ds: String) {
        for (d in ds) {
            roll(d)
        }
    }

    fun roll(d: Char) {
        if (d == 'S') {
            val w = top
            top = north
            north = bottom
            bottom = south
            south = w
        } else if (d == 'N') {
            val w = top
            top = south
            south = bottom
            bottom = north
            north = w
        } else if (d == 'E') {
            val w = top
            top = west
            west = bottom
            bottom = east
            east = w
        } else if (d == 'W') {
            val w = top
            top = east
            east = bottom
            bottom = west
            west = w
        }
    }

    fun top() = this.value.get(top - 1)
    fun east() = this.value.get(east - 1)

    fun setTopAndSouth(n: Int, m: Int) {
        val t = value.indexOf(n) + 1
        val s = value.indexOf(m) + 1

        top = t;
        bottom = 7 - t
        south = s
        north = 7 - s
        if ((top == 1 && south == 2)
            || (top == 2 && south == 6)
            || (top == 6 && south == 5)
            || (top == 5 && south == 1)
        ) {
            east = 3
            west = 4
        } else if ((top == 1 && south == 3)
            || (top == 3 && south == 6)
            || (top == 6 && south == 4)
            || (top == 4 && south == 1)
        ) {
            east = 5
            west = 2
        } else if ((top == 1 && south == 4)
            || (top == 4 && south == 6)
            || (top == 6 && south == 3)
            || (top == 3 && south == 1)
        ) {
            east = 2
            west = 5
        } else if ((top == 1 && south == 5)
            || (top == 5 && south == 6)
            || (top == 6 && south == 2)
            || (top == 2 && south == 1)
        ) {
            east = 4
            west = 3
        } else if ((top == 2 && south == 3)
            || (top == 3 && south == 5)
            || (top == 5 && south == 4)
            || (top == 4 && south == 2)
        ) {
            east = 1
            west = 6
        } else if ((top == 2 && south == 4)
            || (top == 4 && south == 5)
            || (top == 5 && south == 3)
            || (top == 3 && south == 2)
        ) {
            east = 6
            west = 1
        }
    }
}

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val dice = Dice(
        listOf(
            scanner.nextInt(),
            scanner.nextInt(),
            scanner.nextInt(),
            scanner.nextInt(),
            scanner.nextInt(),
            scanner.nextInt()
        )
    )
    val q = scanner.nextInt()
    val answers = mutableListOf<Int>()
    (1..q).forEach {
        dice.setTopAndSouth(scanner.nextInt(), scanner.nextInt())
        answers.add(dice.east())
    }
    answers.forEach {
        println(it)
    }
}