{$apptype console}
{$I-}
type
  tchange=record
    c:char;
    word:string;
  end;
  tmorphism=array[1..1000] of tchange;
  pmorphism=^tmorphism;
var
  morphism:pmorphism;
  nchars:integer;
procedure trim(var s:string);
var
  i:integer;
begin
  i:=length(s);
  while (i>0) do
    if s[i]<>' ' then break
                 else dec(i);
  setlength(s,i);
end;
procedure readmorphism;
var
  f:text;
  i,j:integer;
  s:string;
begin
  assign(f,paramstr(2));
  reset(f);
  readln(f,nchars);
  if ioresult<>0 then halt;
  getmem(morphism,nchars*sizeof(tchange));
  fillchar(morphism^,nchars*sizeof(tchange),0);
  for i:=1 to nchars do
  begin
    readln(f,s);
    trim(s);
    j:=pos('=',s);
    if j<>2 then halt;
    morphism^[i].c:=s[1];
    morphism^[i].word:=copy(s,3,length(s)-2);
  end;
  close(f);
end;
procedure calculatenewword;
var
  f1,f2:text;
  i:integer;
  c:char;
begin
  assign(f1,paramstr(1));
  assign(f2,paramstr(3));
  reset(f1);
  rewrite(f2);
  while not eof(f1) do
  begin
    read(f1,c);
    for i:=1 to nchars do
      if c=morphism^[i].c then
        write(f2,morphism^[i].word);
  end;
  close(f1);
  close(f2);
end;
begin
  if paramcount<>3 then
    exit;
  readmorphism;
  calculatenewword;
end.