let read_lines n =
  let rec read_lines n acc =
    if n = 0 then List.rev acc
    else read_lines (pred n) ((read_line ()) :: acc) in
  read_lines n []

let rec solve () =
  let n_conversion_table = int_of_string @@ read_line () in
  if n_conversion_table = 0 then ()
  else
    let create_conversion_map pairs =
      let hash = Hashtbl.create n_conversion_table in
      List.iter
        (fun (from, to_) -> Hashtbl.add hash from to_)
        pairs;
      hash in
    let conversion_table =
      create_conversion_map
      @@ List.map
           (fun line ->
            Scanf.sscanf line "%s %s" (fun from to_ -> (from, to_)))
           (read_lines n_conversion_table) in
    let convert str =
      try Hashtbl.find conversion_table str with Not_found -> str in
    let n_input = int_of_string @@ read_line () in
    List.iter
      (fun line -> print_string @@ convert line)
      (read_lines n_input);
    print_newline ();
    solve ()

let () = solve ()