{
TASK:LDIST
LANG:PASCAL
}

program LDist;

type TString = array[1..2025] of char;

var D:array[0..255,0..255] of Byte;
    V:array[0..255] of Byte;
    a,b:TString;
    MinCost,LngA,LngB:Integer;


function getmin(const par1,par2:Integer):Integer;
begin
 If (par1 < par2) then getmin := par1
 else getmin := par2;
end;

function GetLengthString(const s:TString):Integer;
var Res:Integer;
begin
 Res := 1;
 while not(s[Res] = Chr(0)) do Inc(Res);
 GetLengthString := Res - 1;
end;

function EqualS(const s1,s2:TString):Boolean;
var L1,L2,iPos:Integer;
begin
 L1 := GetLengthString(s1);
 L2 := GetLengthString(s2);
 If not(L1 = L2) then begin
  EqualS := False;
  Exit;
 end;
 for iPos := 1 to L1 do begin
  If not(s1[iPos] = s2[iPos]) then begin
   EqualS := False;
   Exit;
  end;
 end;
 Equals := True;
end;


function ReplaceString(var s:TString;const pos:Word;const ch:Char):Integer;
var Cost:Integer;
begin
 Cost := getmin(D[Ord(s[pos]),Ord(ch)] , V[Ord(s[pos])] + V[Ord(ch)]);
 s[pos] := ch;
 ReplaceString := Cost;
end;

function DeleteString(var s:TString;const pos:Word):Integer;
var Lng,Cost:Integer;
    ipos:Word;
begin
 Cost := V[Ord(s[pos])];
 Lng := GetLengthString(s);
 for iPos := pos to Lng - 1 do s[iPos] := s[iPos + 1];
 s[Lng] := Chr(0);
 DeleteString := Cost;
end;

function InsertString(var s:TString;const pos:Word;const ch:Char):Integer;
var Lng,iPos:Integer;
begin
 Lng := GetLengthString(s);
 for iPos := Lng + 1 downto pos + 1 do s[iPos] := s[iPos - 1];
 s[pos] := ch;
 InsertString := V[Ord(ch)];
end;


procedure Deep(const pos:Word;s:TString;value:Integer);
var news:TString;
    D1,D2,D3:Integer;
begin
 If (EqualS(s,b)) then begin
  If (MinCost > Value) or (MinCost = - 1) then MinCost := Value;
  Exit;
 end;


 If (s[pos] = b[pos]) then Deep(pos + 1,s,Value)
 else begin

  If (GetLengthString(s) < LngB) then begin
   news := s;
   D1 := InsertString(news,pos,b[pos]);
   Deep(pos + 1,news,Value + D1);
  end;

  If (GetLengthString(s) > LngB) then begin
   news := s;
   D1 := DeleteString(news,pos);
   Deep(pos + 1,news,Value + D1);
  end;

  news := s;
  D2 := ReplaceString(news,pos,b[pos]);
  If (pos = LngB) and not(Equals(s,b)) then Exit;
  Deep(pos + 1,news,Value + D2);

 end;
end;

procedure Input;
var P,iP,Value:Byte;
    Q,iQ:Integer;
    ch,ch1,ch2:char;
begin
 FillChar(D,SizeOf(D),100);
 FillChar(V,SizeOf(V),100);
 FillChar(a,SizeOf(a),Chr(0));
 FillChar(b,SizeOf(b),Chr(0));

 Readln(P);
 If (P > 0) then begin
  for iP := 1 to P do begin
   Readln(ch,Value);
   V[Ord(ch)] := Value;
  end;
 end;

 Readln(Q);
 If (Q > 0) then begin
  for iQ := 1 to Q do begin
   Readln(ch1,ch,ch2,Value);
   D[Ord(ch1),Ord(ch2)] := Value;
   D[Ord(ch2),Ord(ch1)] := Value;
  end;
 end;

 Readln(a);
 LngA := GetLengthString(a);
 Readln(b);
 LngB := GetLengthString(b);
end;

begin
 Input;
 MinCost := - 1;
 Deep(1,a,0);
 If not(MinCost = - 1) then Writeln(MinCost)
 else Writeln(0);
 Readln;
end.
