class dice init = object (self)
  val mutable d:int*int*int*int*int*int = init
  val mutable p:'a list = []

  method private tn (x1,x2,x3,x4,x5,x6):int*int*int*int*int*int = (x2,x6,x3,x4,x1,x5)
  method private ts l = self#tn l |> self#tn |> self#tn
  method private te (x1,x2,x3,x4,x5,x6):int*int*int*int*int*int = (x4,x2,x1,x6,x5,x3)
  method private tw l = self#te l |> self#te |> self#te

  method right x y =
    let rec find l = match l with
        [] -> raise Not_found
      | (x1,x2,x3,x4,x5,x6)::tl -> if x = x1 && y = x2 then x3 else find tl
    in find p

  method gen =
    let rec rotate d = function
        0 -> ()
      | i -> p <- (d::p);rotate (self#te d) (i-1) in
    let rotate2 n = rotate n 4 in
    rotate2 d;
    rotate2 (self#te d |> self#ts);
    rotate2 (self#tw d |> self#ts);
    rotate2 (self#tn d);
    rotate2 (self#ts d);
    rotate2 (self#tn d |> self#tn)
end;;

let split_int s = Str.split (Str.regexp_string " ") s |> List.map int_of_string;;

let () =
  let n = read_line () |> split_int |> List.nth and
      ct = read_int () in
  let l = (n 0,n 1,n 2,n 3,n 4,n 5) in
  let d = new dice l in
  let () = d#gen in
  let rec loop = function
      0 -> ()
    | i -> let read =  read_line () |> split_int |> List.nth in
           Printf.printf "%d\n" (d#right (read 0) (read 1));loop (i-1)
  in loop ct;;