program wordperm;
uses crt;
label prase;
const max=64;{maximalni delka slova}
var
 F: text;
 a: char;{nemuselo by to tu ani byt myslim}
 b: array [0..max] of char;{sem ukladam slova}
 c: array [0..max] of char;{sem referencni slovo}
 d: array [0..max] of char;{sem ulozim aktualni slovo}
 e: array [0..max] of char;
 i,j:integer;{holky sikovny}

Procedure IsPerm(x,y,edge:integer);
var pom:integer;
begin
if x=y then
  begin
  if (x=0) then {vsechna pismena byla obsazena, nasleduje vypis slova}
  begin
    for pom:=1 to edge do
    begin
      write(d[pom]);
      e[pom]:=c[pom];
    end;
    writeln;
  end
  else
  for pom:=x downto 1 do {}
    begin
    if e[pom]=b[x] then
      begin
      e[pom]:=e[x];
      d[x]:=b[x];
      IsPerm((x-1),(y-1),edge);
      end;
    end;
  end;
end;

{------------ZACATEK TELA PROGRAMU----------}

begin
 {$I-}
 Assign(F, 'c:\temp\test.txt');
 Reset(F);
 {$I+}

while (not eof(F)) and (a=' ') do
read(F,a);

j:=0;

{nacteni prvniho slova}
while not( eof(F)) do
  begin
  read (F,a);
  if (a<>' ') then
    begin
    j:=j+1;
    c[j]:=a;
    e[j]:=c[j];
   { write(c[j]);}
    end
  else break
  end;

i:=0;

while not (eof(F)) do
  begin
  read(F,a);

 {cteme pokud se nactene cislo nerovna mezere}
  if (a<>' ') then
    begin
    i:=i+1;
    b[i]:=a;
{    write(b[i]);}
    end
  else
    begin
    IsPerm(j,i,j);
    i:=0;
    end;
  end;
  IsPerm(j,i,j);
  close(F);
readln;
end.