{
TASK:GEN
LANG:PASCAL
}


type ss = string[100];

var h, s : ss;
    xx, fl, ii, jj, n, m ,k, nn  : longint;
    c, c1, c2, c3 : char;
    a : array['A'..'Z']of set of char;
    bbr, abr : array[1..100]of longint;
    b : array['A'..'Z', 1..100]of string[2];
    otg : array['A'..'Z']of byte;

Procedure readin;
begin
  readln( h);
  readln( nn);

  for ii:=1 to nn do begin
    readln( c1, c2);
    a[c1]:= a[c1] + [c2];
  end;

  readln( m);
  for ii:=1 to m do begin
    readln( c1, c2, c3);
    s:=c2+c3;
    inc(bbr[ord(c1)-64]);
    b[c1, bbr[ord(c1)-64]]:=s;
  end;

end;

Procedure test(v : ss);
begin
  fl:=0;
  for jj:=1 to n do begin
    c1:=v[jj];
    c2:=h[jj];
    if c2 in a[c1] then
    else fl:=1;
  end;
  if fl=0 then begin
    otg[c]:=1;
    xx:=1;
  end;

end;



Procedure make(s : ss);
var l, i, j, p, q: longint;
    s1 : ss;
    cc : char;
    d : string[2];

begin
  l:=length(s);
  if l=n then begin test(s); exit; end;
  for i:=1 to l do begin
    p:=bbr[ord(s[i])-64];
    if p>0 then begin
      for j:=1 to p do begin
        s1:=copy(s, 1, i-1);
        s1:=s1+b[s[i], j];
        s1:=s1 + copy(s, i+1, l-i);
        make(s1);
      end;
    end;
  end;
end;

Begin
  readin;
  n:=length(h);
  xx:=0;
  for c:='A' to 'Z' do begin
    make(c);

  end;
  if xx=0 then write(0)
  else for c:='A' to 'Z' do if otg[c]=1 then write(c);
  writeln;
End.