module Main where

import Data.Functor
import Data.Maybe

getInts :: IO [Int]
getInts = map read . words <$> getLine

main :: IO ()
main = do
  { fs@[t,s,e,w,n,b] <- getInts
  ; [q] <- getInts
  ; let iftab = zip [1..] fs
  ; let fitab = zip fs [1..]
  ; loop q iftab fitab  
  }

loop :: Int -> [(Int,Int)] -> [(Int,Int)] -> IO ()
loop 0 _  _   = done
loop m iftab fitab = do
  { [t,s] <- getInts
  ; let x = maybe undefined id (lookup t fitab)
  ; let y = maybe undefined id (lookup s fitab)
  ; let z = maybe undefined id (lookup (east (x,y)) iftab)
  ; print z >> loop (m-1) iftab fitab
  }

done :: IO ()
done = return ()

east (1,2) = 3
east (1,3) = 5
east (1,4) = 2
east (1,5) = 4
east (2,1) = 4
east (2,3) = 1
east (2,4) = 6
east (2,6) = 3
east (3,1) = 2
east (3,2) = 6
east (3,5) = 1
east (3,6) = 5
east (4,1) = 5
east (4,2) = 1
east (4,5) = 6
east (4,6) = 2
east (5,1) = 3
east (5,3) = 6
east (5,4) = 1
east (5,6) = 4
east (6,2) = 4
east (6,3) = 2
east (6,4) = 5
east (6,5) = 3