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 numbers = io.StdIn.readLine().split(" ")
    val q = io.StdIn.readInt()
    (0 until q)
      .map(_ => io.StdIn.readLine().split(" "))
      .foreach { case Array(u, s) =>
        val iu = numbers.indexWhere(_ == u)
        val is = numbers.indexWhere(_ == s)
        val ie = Dice(Sides(iu), !Sides(is)).eastSide.index
        println(numbers(ie))
      }
  }

  sealed trait Sides {
    def index: Int = Sides.values.indexWhere(_ == this)

    def unary_! : Sides = this match {
      case Sides.One   => Sides.Six
      case Sides.Two   => Sides.Five
      case Sides.Three => Sides.Four
      case Sides.Four  => Sides.Three
      case Sides.Five  => Sides.Two
      case Sides.Six   => Sides.One
    }
  }
  object Sides {
    case object One extends Sides
    case object Two extends Sides
    case object Three extends Sides
    case object Four extends Sides
    case object Five extends Sides
    case object Six extends Sides

    val values: Array[Sides] = Array(One, Two, Three, Four, Five, Six)
    def apply(index: Int): Sides = values(index)
  }

  case class Dice(upSide: Sides, northSide: Sides) {
    val eastSide: Sides = Dice.eastSides(upSide, northSide)
    def downSide: Sides = !upSide
    def southSide: Sides = !northSide
    def westSide: Sides = !eastSide

    def rotate(direction: Direction): Dice = direction match {
      case Direction.North => Dice(southSide, upSide)
      case Direction.South => Dice(northSide, downSide)
      case Direction.East => Dice(westSide, northSide)
      case Direction.West => Dice(eastSide, northSide)
    }
  }
  object Dice {
    import Sides._
    def apply(): Dice = Dice(One, Five)

    private[this] val eastSidesMap: Map[(Sides, Sides), Sides] = {
      def toMap(roll: Array[Sides], sides: Sides): Map[(Sides, Sides), Sides] = {
        roll.zip(roll.tail).map(_ -> sides).toMap
      }

      val _4 = Array[Sides](One, Two, Six, Five, One)
      val _2 = Array[Sides](One, Three, Six, Four, One)
      val _6 = Array[Sides](Two, Three, Five, Four, Two)

      toMap(_4, Four) ++ toMap(_4.reverse, Three) ++
      toMap(_2, Two)  ++ toMap(_2.reverse, Five) ++
      toMap(_6, Six)  ++ toMap(_6.reverse, One)
    }

    def eastSides(upSide: Sides, northSide: Sides): Sides = eastSidesMap((upSide, northSide))
  }

  sealed trait Direction
  object Direction {
    case object North extends Direction
    case object South extends Direction
    case object East extends Direction
    case object West 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
    }
  }
}