{
Written by Fyodor Menshikov 18.02.2005
7:11-7:16
}
const
   key:array['A'..'Z']of char=(
      '2','2','2',
      '3','3','3',
      '4','4','4',
      '5','5','5',
      '6','6','6',
      '7','7','7','7',
      '8','8','8',
      '9','9','9','9'
   );
var
   s,ss,digits:string;
   i,j,n:longint;
begin
   assign(input,'input.txt');
   reset(input);
   assign(output,'output.txt');
   rewrite(output);
   readln(digits);
   readln(n);
   for i:=1 to n do begin
      readln(s);
      if length(s)=length(digits) then begin
         ss:=s;
         for j:=1 to length(s) do
            s[j]:=key[s[j]];
         if s=digits then
            writeln(ss);
      end;
   end;
end.