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)

object Main {
  def main(args: Array[String]): Unit = {
    val sides = io.StdIn.readLine().split(" ").map(_.toInt)
    val dice = Dice(sides(0), sides(1), sides(2), sides(3), sides(4), sides(5))
    val q = io.StdIn.readInt()
    (0 until q).map(_ => io.StdIn.readLine().split(" ").map(_.toInt)).foreach { case Array(up, south) =>
      println(
        detectEast(dice, sides.indexWhere(_ == up), sides.indexWhere(_ == south))
      )
    }
  }

  private[this] val eastIndexes: Map[(Int, Int), Int] = {
    def toIndexMap(around: Array[Int], east: Int): Map[(Int, Int), Int] = {
      around.zip(around.tail)
          .map { case (up, south) => (up - 1, south - 1) -> (east - 1) }
          .toMap
    }

    val _1 = Array(2, 3, 5, 4, 2)
    val _6 = _1.reverse
    val _3 = Array(1, 2, 6, 5, 1)
    val _4 = _3.reverse
    val _2 = Array(1, 4, 6, 3, 1)
    val _5 = _2.reverse

    toIndexMap(_1, 1) ++ toIndexMap(_6, 6) ++
        toIndexMap(_3, 3) ++ toIndexMap(_4, 4) ++
        toIndexMap(_2, 2) ++ toIndexMap(_5, 5)
  }

  def detectEast(dice: Dice, upIndex: Int, southIndex: Int): Int = {
    import dice._
    eastIndexes.get((upIndex, southIndex)) match {
      case Some(0) => upSide
      case Some(1) => southSide
      case Some(2) => eastSide
      case Some(3) => westSide
      case Some(4) => northSide
      case Some(5) => downSide
      case _ => -1
    }
  }

  case class Dice(upSide: Int, southSide: Int, eastSide: Int, westSide: Int, northSide: Int, downSide: Int) {
    import Direction._
    def spinClockwise: Dice = Dice(upSide, eastSide, northSide, southSide, westSide, downSide)
    def spinCounterclockwise: Dice = Dice(upSide, westSide, southSide, northSide, eastSide, downSide)
    def roll(direction: Direction): Dice = direction match {
      case North =>
        Dice(southSide, downSide, eastSide, westSide, upSide, northSide)
      case South =>
        Dice(northSide, upSide, eastSide, westSide, downSide, southSide)
      case West =>
        Dice(eastSide, southSide, downSide, upSide, northSide, westSide)
      case East =>
        Dice(westSide, southSide, upSide, downSide, northSide, eastSide)
    }
  }

  sealed trait Direction
  object Direction {
    case object North extends Direction
    case object South extends Direction
    case object West extends Direction
    case object East extends Direction

    def apply(c: Char): Direction = c match {
      case 'N' => North
      case 'S' => South
      case 'E' => East
      case 'W' => West
      case _ => throw new Exception
    }
  }
}