{
TASK:gen
LANG:PASCAL
}

uses dos;

const
  infile = '';
  outfile = '';

var
  f: text;

  stt : longint;

  ch : char;
  st, st2 : string;
  ps : byte;
  fnd : boolean;
  lst, i, j, n1, n2 : longint;

  op1, op2 : array[0..101] of string[3];
  cnt1, cnt2 : array['A'..'Z'] of integer;

  aop1 : array['A'..'Z', 0..100] of string[1];
  aop2 : array['A'..'Z', 0..100] of string[2];
  ops  : array['A'..'Z', 'a'..'z'] of boolean;

  xh, xm, xs, xhh : word;
function time : longint;
begin
  gettime(xh, xm, xs, xhh);
  time:=(xh*3600 + xm * 60 + xs)*100+xhh;
end;

function upp(a : char) : char;
begin
  if ord(a) >= 97 then upp:=chr(ord(a)-32) else upp:=a;
end;

function rec(ss : string[101]; l : byte) : boolean;
var p, p2 : byte;
begin
//  writeln(ss,' ',ps, ' ',l);
//  readln;
//  if ss = 'AABABB' then begin write(ss, ' ', ps, ' ', l); readln; end;
  rec:=false;
  if time - stt > 90 then exit;
  if l >= lst then
  begin
    j:=1;
    for i:=1 to lst do
      if not ops[ss[i], st[i]] then begin j:=0; break; end;
    if j = 1 then begin rec:=true; end
    else rec:=false;
  end
  else
  begin
    p2:=ps;
    while (ss[ps] = st2[ps])and(ps+1 < l) do
    begin
      inc(ps);
      if rec(ss, l) then begin rec:=true; exit; end;
    end;
    ps:=p2;
    for p:=1 to cnt2[ss[ps]] do
    begin
      if ps < l then
      begin
//        if (cach[ch, ps, p, ss[ps], ss[ps+1]]) then continue;
//        cach[ch, ps, p, ss[ps], ss[ps+1]]:=true;
      end;
      if rec(copy(ss, 1, ps-1)+aop2[ss[ps], p]+copy(ss, ps+1, l-ps), l+1) then
        begin rec:=true; break; end
    end;
  end;
end;

procedure input;
begin
  assign(f, infile);
  reset(f);
  readln(f, st);
  readln(f, n1);
  for i:=1 to n1 do
  begin
    readln(f, op1[i]);
    inc(cnt1[op1[i][1]]);
    aop1[op1[i][1], cnt1[op1[i][1]]]:=op1[2];
    ops[op1[i,1], op1[i,2]]:=true;
  end;
  readln(f, n2);
  for i:=1 to n2 do
  begin
    readln(f, op2[i]);
    inc(cnt2[op2[i][1]]);
    aop2[op2[i,1], cnt2[op2[i,1]]]:=copy(op2[i], 2, 2);
  end;
  close(f);
end;

begin
  stt:=time;
  input;
  lst:=length(st);
  for i:=1 to lst do st2[i]:=upp(st[i]);

  if length(st) = 1 then
  begin
    if cnt1[upp(st[1])] <> 0 then writeln(upp(st[1])) else writeln(0);
  end
  else
  begin
    for ch:='A' to 'Z' do
    begin
      ps:=1;
//      writeln(ch);
      if rec(ch, 1) then
      begin
        fnd:=true;
        write(ch);
      end;
    end;
    if fnd then writeln else writeln(0);
  end;
end.
