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