import scala.io.StdIn
import scala.io.Source

object Main{
  def main(args: Array[String]){
    val label = StdIn.readLine.split(" ").toList.map(_.toInt)
    val q = Iterator.continually(StdIn.readLine).takeWhile(_ != null).toList
    val dice = new Dice(label)

    println(q.tail.map(_.split(" ").toList.map(_.toInt)).map(dice.findRight(_)).mkString("\n"))
  }
}

class Dice(sur: List[Int]){
  val index: List[Int] = sur

  def findRight(list: List[Int]): Int={
    val dice = new Dice(index)
    val operators = List[String]("frontrear", "leftright", "basetop")
    val kindOfDices = List[Dice](dice, dice.rotN, dice.rotS, dice.rotW, dice.rotE, dice.rotN.rotN)

    val finddice = for{
      kindOfDice <- kindOfDices
      operator <- operators
    }yield kindOfDice.rotN.rotR(operator).map{
      _ match {
        case x if x.getVal(0) == list(0) && x.getVal(1) == list(1) => x.getVal(2)
        case _ => 0
      }
    }.sum

    finddice.max
  }

  def getVal(i: Int): Int ={
    index(i)
  }

  def rotR(op:String): List[Dice] ={
    op match{
      case x if(x == "frontrear") =>
        List[Dice](
          new Dice(List[Int](index(0), index(1), index(2), index(3), index(4), index(5))),
          new Dice(List[Int](index(3), index(1), index(0), index(5), index(4), index(2))),
          new Dice(List[Int](index(5), index(1), index(3), index(2), index(4), index(0))),
          new Dice(List[Int](index(2), index(1), index(5), index(0), index(4), index(3)))
        )
      case x if(x == "leftright") =>
        List[Dice](
          new Dice(List[Int](index(0), index(1), index(2), index(3), index(4), index(5))),
          new Dice(List[Int](index(5), index(1), index(2), index(3), index(5), index(1))),
          new Dice(List[Int](index(5), index(4), index(2), index(3), index(1), index(0))),
          new Dice(List[Int](index(1), index(5), index(2), index(3), index(0), index(4)))
     )
    case x if(x == "basetop") =>
      List[Dice](
        new Dice(List[Int](index(0), index(1), index(2), index(3), index(4), index(5))),
        new Dice(List[Int](index(0), index(3), index(1), index(4), index(2), index(5))),
        new Dice(List[Int](index(0), index(2), index(4), index(1), index(3), index(5))),
        new Dice(List[Int](index(0), index(4), index(3), index(2), index(1), index(5)))
      )
    case _ => List()
    }
  }

  def rotN(): Dice ={
    new Dice(List[Int](index(1), index(5), index(2), index(3), index(0), index(4)))
  }

  def rotS(): Dice ={
    new Dice(List[Int](index(4), index(0), index(2), index(3), index(5), index(1)))
  }

  def rotW(): Dice ={
    new Dice(List[Int](index(2), index(1), index(5), index(0), index(4), index(3)))
  }

  def rotE(): Dice ={
    new Dice(List[Int](index(3), index(1), index(0), index(5), index(4), index(2)))
  }
}