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

import scala.io.StdIn.{readLine, readInt}

object Main extends App {

  val labels = readLine().split(" ")
  val q = readInt()

  val dice: Dice = new Dice(labels)
  for (_ <- 1 to q) {
    val Array(topLabel, frontLabel) = readLine().split(" ")
    dice.setFace(topLabel, frontLabel)
    println(dice.getLabel(2))
  }

  class Dice(labels: Array[String]) {

    object f extends Enumeration {
      val top = 0
      val front = 1
      val right = 2
      val left = 3
      val back = 4
      val bottom = 5
    }

    val faces = Array.fill(6){0}
    for (i <- 0 until faces.length) {
      faces(i) = i
    }

    def getLabel(i: Int): String = {
      labels(faces(i))
    }

    def setFace(topLabel: String, frontLabel: String): Boolean = {
      var currentPos = getCurrentPos(topLabel)
      currentPos match {
        case p if p == faces(f.top) => {}
        case p if p == faces(f.front) => move("N")
        case p if p == faces(f.right) => move("W")
        case p if p == faces(f.left) => move("E")
        case p if p == faces(f.back) => move("S")
        case p if p == faces(f.bottom) => move("EE")
        case -1 => return false
      }
      currentPos = getCurrentPos(frontLabel)
      currentPos match {
        case p if p == faces(f.top) => return false
        case p if p == faces(f.front) => {}
        case p if p == faces(f.right) => move("SEN")
        case p if p == faces(f.left) => move("SWN")
        case p if p == faces(f.back) => move("SEEN")
        case p if p == faces(f.bottom) => return false
        case -1 => return false
      }
      true
    }

    def getCurrentPos(label: String): Int = {
      for (i <- 0 until labels.length) {
        if (labels(faces(i)).equals(label)) {
          return faces(i)
        }
      }
      -1
    }

    def move(direction: String): Unit = {
      direction.foreach(move(_))
    }

    def move(direction: Char): Unit = {
      val oldTop = faces(f.top);
      direction match {
        case 'S' => {
          faces(f.top) = faces(f.back)
          faces(f.back) = faces(f.bottom)
          faces(f.bottom) = faces(f.front)
          faces(f.front) = oldTop
        }
        case 'N' => {
          faces(f.top) = faces(f.front)
          faces(f.front) = faces(f.bottom)
          faces(f.bottom) = faces(f.back)
          faces(f.back) = oldTop
        }
        case 'W' => {
          faces(f.top) = faces(f.right)
          faces(f.right) = faces(f.bottom)
          faces(f.bottom) = faces(f.left)
          faces(f.left) = oldTop
        }
        case 'E' => {
          faces(f.top) = faces(f.left)
          faces(f.left) = faces(f.bottom)
          faces(f.bottom) = faces(f.right)
          faces(f.right) = oldTop
        }
      }
    }

    def isIdentical(other: Dice): Boolean = {
      for (i <- 0 until labels.length; j <- 0 until labels.length) {
        val success1 = this.setFace(labels(i), labels(j))
        val success2 = other.setFace(labels(i), labels(j))
        if (success1 && success2 && checkIdentical(other)) {
          return true
        }
      }
      false
    }

    def checkIdentical(other: Dice): Boolean = {
      for (i <- 0 until labels.length) {
        if (!getLabel(i).equals(other.getLabel(i))) { return false }
      }
      true
    }
  }

}