open Printf
open Scanf

let id x = x
let tuple x y = (x, y)
             
module Dice =
  struct
    type t = {tp:int; ft:int; lt:int; rt:int; bk:int; bm:int}

    let create x1 x2 x3 x4 x5 x6 =
      {tp = x1; ft = x2; lt = x3; rt = x4; bk = x5; bm = x6}

    let roll d = function
        'S' -> {d with tp = d.bk; ft = d.tp; bm = d.ft; bk = d.bm}
      | 'E' -> {d with tp = d.rt; lt = d.tp; bm = d.lt; rt = d.bm}
      | 'W' -> {d with tp = d.lt; rt = d.tp; bm = d.rt; lt = d.bm}
      | 'N' -> {d with tp = d.ft; bk = d.tp; bm = d.bk; ft = d.bm}
      | _ -> failwith "roll"

    let rollx d dirs =
      let rd = ref d in
      String.iter (fun c -> rd := roll !rd c) dirs;
      !rd

    let top d = d.tp
    let front d = d.ft
    let left d = d.lt
  end

let () =
  let d = scanf "%d %d %d %d %d %d " Dice.create in
  let q = scanf "%d " id in
  let rec loop x =
    if x = 0 then ()
    else
      let tp, ft = scanf "%d %d " tuple in
      let ht = Hashtbl.create 100 in
      let rec dfs u =
        if Hashtbl.mem ht u then None
        else
          if Dice.top u = tp && Dice.front u = ft then Some u
          else begin
              Hashtbl.add ht u true;
              List.fold_left (fun vo c ->
                              match dfs (Dice.roll u c) with
                                Some v -> Some v
                              | None -> vo
                             ) None ['N'; 'S'; 'E'; 'W']
            end
      in
      (match dfs d with
         None -> failwith "dfs"
       | Some d' -> printf "%d\n" (Dice.left d'));
      loop (x-1)
  in
  loop q