/**
  * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_11_B&lang=jp
  * Structure and Class - Dice II
  */
object Main {

  class Dice(numbers: IndexedSeq[Int]) {
    def apply(number: Int): Int = {
      numbers(number)
    }

    def rotateTo(direction: Char): Dice = {
      direction match {
        case 'N' => new Dice(IndexedSeq(numbers(1), numbers(5), numbers(2), numbers(3), numbers(0), numbers(4)))
        case 'S' => new Dice(IndexedSeq(numbers(4), numbers(0), numbers(2), numbers(3), numbers(5), numbers(1)))
        case 'W' => new Dice(IndexedSeq(numbers(2), numbers(1), numbers(5), numbers(0), numbers(4), numbers(3)))
        case 'E' => new Dice(IndexedSeq(numbers(3), numbers(1), numbers(0), numbers(5), numbers(4), numbers(2)))
      }
    }

    def find(number: Int): Int = {
      numbers.indexWhere(_ == number)
    }
  }

  def main(args: Array[String]): Unit = {
    val dice = new Dice(io.StdIn.readLine().split(" ").map(_.toInt))
    val questions = io.StdIn.readInt()
    Iterator.continually(io.StdIn.readLine()).takeWhile(_ != null).map(_.split(" ").map(_.toInt))
      .take(questions)
      .map {
        case Array(top, front) => determineRight(dice, top, front)
      }
      .foreach(println)
  }

  def determineRight(dice: Dice, top: Int, front: Int): Int = {
    val operationsToTop = Seq("", "N", "W", "E", "S", "NN")
    val operationsToFront = Seq("S", "", "WSE", "ESW", "SEEN", "N")
    val dice2 = operationsToTop(dice.find(top)).foldLeft(dice)(_.rotateTo(_))
    val dice3 = operationsToFront(dice2.find(front)).foldLeft(dice2)(_.rotateTo(_))
    dice3(2)
  }
}