type dice = { one : int; two : int; three : int;
              four : int; five : int; six : int}

let right_side {one=a;two=b;three=c;four=d;five=e;six=f} (top,front) =
  if top=a then (if front=b then c
                 else if front=c then e
                 else if front=d then b
                 else d)
  else if top=b then (if front=a then d
                      else if front=c then a
                      else if front=d then f
                      else c)
  else if top=c then (if front=a then b
                      else if front=b then f
                      else if front=e then a
                      else e)
  else if top=d then (if front=a then e
                      else if front=b then a
                      else if front=e then f
                      else b)
  else if top=e then (if front=a then c
                      else if front=c then f
                      else if front=d then a
                      else d)
  else (if front=b then d
        else if front=c then b
        else if front=d then e
        else c)

let () =
  let dice = Scanf.scanf "%d %d %d %d %d %d\n"
      (fun a b c d e f -> {one=a;two=b;three=c;four=d;five=e;six=f}) in
  let n = read_int () in
  let rec loop = function
      0 -> ()
    | i -> Scanf.scanf "%d %d\n" (fun a b -> a,b)
           |> right_side dice
           |> Printf.printf "%d\n";
      loop (i-1)
  in loop n