
object Main extends App {

  override def main(args: Array[String]): Unit = {
    val str = io.StdIn.readLine.split(' ')

    val init = new Dice(str(0), str(1), str(2), str(3), str(4), str(5))
    val q = io.StdIn.readLine.toInt

    (1 to q).map(_ => io.StdIn.readLine.split(' ')).map(i => (i(0), i(1)))
      .map(topSouth => init.getAsTopAndSouthIs(topSouth._1, topSouth._2))
      .foreach(d => println(d.east));

  }

  case class Dice(top: String, south: String, east: String, west: String, north: String, buttom: String) {

    def toNorth: Dice = {
      new Dice(south, buttom, east, west, top, north)
    }
    def toSouth: Dice = {
      new Dice(north, top, east, west, buttom, south)
    }
    def toEast: Dice = {
      new Dice(west, south, top, buttom, north, east)
    }
    def toWest: Dice = {
      new Dice(east, south, buttom, top, north, west)
    }
    def rotRight: Dice = {
      new Dice(top, east, north, south, west, buttom)
    }
    def rotLeft: Dice = {
      new Dice(top, west, south, north, east, buttom)
    }

    def getAsTop(toTop:String):Dice = {
       toTop match{
         case str if str==top => this
         case str if str==east => toWest
         case str if str==west => toEast
         case str if str==south => toNorth
         case str if str==north => toSouth
         case str if str==buttom => toNorth.toNorth
       }
    }
    
    def getAsSouthByRot(toSouth:String) :Dice = {
      toSouth match{
        case str if str==south => this
        case str if str==east => rotRight
        case str if str==west => rotLeft
        case str if str==north => rotRight.rotRight
      }
    }
    
    def getAsTopAndSouthIs(top: String, south: String): Dice = {
      getAsTop(top).getAsSouthByRot(south)
    }

  }

}