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 read_surface () =
  String.concat "" (read_lines 8)

let contains_substring s t =
  let len = String.length t in
  let n = (String.length s) - len + 1 in
  let rec loop i =
    if i >= n then false
    else if (String.sub s i len) = t then true
    else loop (succ i) in
  loop 0

let recognize_pattern surface =
  snd @@ List.find
    (fun (p, _) -> contains_substring surface p)
    [
      ("1100000011", "A");
      ("10000000100000001000000010000000", "B");
      ("1111", "C");
      ("1000000110000001", "D");
      ("11000000011", "E");
      ("100000001100000001", "F");
      ("110000011", "G");
    ]

let () =
  let rec loop () =
    print_endline @@ recognize_pattern @@ read_surface ();
    ignore @@ read_line ();
    loop ()
  in
  try loop () with End_of_file -> ()