import scala.io.StdIn.{readLine,readInt}
import scala.collection.mutable.ArrayBuffer

case class Dice(var d:Array[String]) {
  private val s = Array(4,0,2,3,5,1)
  private val w = Array(2,1,5,0,4,3)
  private val h = Array(0,2,4,1,3,5)
  private var all = Array.empty[Array[String]]

  def rotateAll() = {
    val buf = ArrayBuffer.empty[Array[String]]

    buf += d
    buf += s.map(d)
    buf += s.map(s).map(d)
    buf += s.map(s).map(s).map(d)
    buf += w.map(d)
    buf += w.map(w).map(w).map(d)

    buf += h.map(d)
    buf += s.map(s).map(s).map(h).map(d)
    buf += w.map(h).map(d)
    buf += w.map(w).map(w).map(h).map(d)
    buf += s.map(h).map(d)
    buf += s.map(s).map(h).map(d)

    buf += h.map(h).map(d)
    buf += s.map(s).map(s).map(h).map(h).map(d)
    buf += w.map(h).map(h).map(d)
    buf += w.map(w).map(w).map(h).map(h).map(d)
    buf += s.map(h).map(h).map(d)
    buf += s.map(s).map(h).map(h).map(d)

    buf += h.map(h).map(h).map(d)
    buf += s.map(s).map(s).map(h).map(h).map(h).map(d)
    buf += w.map(h).map(h).map(h).map(d)
    buf += w.map(w).map(w).map(h).map(h).map(h).map(d)
    buf += s.map(h).map(h).map(h).map(d)
    buf += s.map(s).map(h).map(h).map(h).map(d)
    all = buf.toArray
  }

  def getAll = all

  override def toString() = d(0)
}

object Main extends App {
  val faces = readLine.split(" ")
  val n = readInt
  val dice = Dice(faces)

  dice.rotateAll()
  (0 until n).foreach { s =>
    val l =  readLine.split(" ")
    println(dice.getAll.find(t => t(0) == l(0) && t(1) == l(1)).get(2))
  }
}