import Data.List
import Data.Maybe
import Control.Monad
main = do
  xs <- (return . map (read::String->Int) . words) =<< getLine
  q <- readLn::IO Int
  forM [1..q] $ \i -> do
    uf <- (return . map (read::String->Int) . words) =<< getLine
    print $ xs !! (f3 xs uf)
  
f1 xs x = (fromJust . (elemIndex x)) xs
f2 xs u | f1 xs u == 0 = [1,2,4,3]
f2 xs u | f1 xs u == 1 = [0,3,5,2]
f2 xs u | f1 xs u == 2 = [0,1,5,4]
f2 xs u | f1 xs u == 3 = [0,4,5,1]
f2 xs u | f1 xs u == 4 = [0,2,5,3]
f2 xs u | f1 xs u == 5 = [1,3,4,2]
f3 xs [u,f] = (f2 xs u) !! (((f1 (f2 xs u) (f1 xs f)) + 1) `mod` 4)
