let () =
  let faces = Scanf.sscanf (read_line ()) "%d %d %d %d %d %d" (fun a b c d e f ->  [a;b;c;d;e;f] ) in
  let n = read_int () in
  let rec loop = function
      0 -> ()
    | i -> let index_of_face face =
             let rec loop i = function
                 [] -> raise Not_found
               | hd::tl -> if face = hd then i
                           else loop (i+1) tl
             in
             loop 0 faces
           in
           let i_top, i_front = 
             let top, front = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> a,b ) in
             index_of_face top, index_of_face front
           in
           let i_right = 
             if i_front = (i_top + 2) mod 6 then (i_top + 4) mod 6 
             else if i_front = (i_top + 4) mod 6 then (9 - i_top) mod 6 
             else if i_front = (9 - i_top) mod 6 then (7 - i_top) mod 6 
             else if i_front = (7 - i_top) mod 6 then (i_top + 2) mod 6
             else raise (Invalid_argument "This is not dice")
           in
           Printf.printf "%d\n" @@ List.nth faces i_right;
           loop (i-1)
  in
  loop n
;;
