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
    }
  }
}