var
  num,s: string;
  i,j,n: Integer;
  ok: boolean;
begin

//  assign(input,'input.txt'); reset(input);
//  assign(output,'output.txt'); rewrite(output);

  readln(num);
  readln(n);
  for i:=1 to n do
  begin
    readln(s);
    if length(s)<>length(num) then continue;
    ok:=true;
    for j:=1 to length(s) do 
    begin
      if not(
         ((s[j] in ['A','B','C','a','b','c'])and(num[j]='2'))or
         ((s[j] in ['D','E','F','d','e','f'])and(num[j]='3'))or
         ((s[j] in ['G','H','I','g','h','i'])and(num[j]='4'))or
         ((s[j] in ['J','K','L','j','k','l'])and(num[j]='5'))or
         ((s[j] in ['M','N','O','m','n','o'])and(num[j]='6'))or
         ((s[j] in ['P','Q','R','S','p','q','r','s'])and(num[j]='7'))or
         ((s[j] in ['T','U','V','t','u','v'])and(num[j]='8'))or
         ((s[j] in ['W','X','Y','Z','w','x','y','z'])and(num[j]='9')) 
        )
      then begin
        ok := false; break;
      end;
    end;
    if ok then writeln(s);
  end;
end.
