Problem: Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings. 

Input

The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

Output

If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

Examples

Input

uayd
uxxd
3
a x 8
x y 13
d c 3


Output

21
uxyd


Input

a
b
3
a b 2
a b 3
b a 5


Output

2
b


Input

abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0


Output

-1

Solution (in PYTHON):

Solution:


stdin_flag=1
if not stdin_flag:
  read_line_index=0
  testcasefilename="test.txt"
  Stestcase=open(testcasefilename).read()
  read_line_datas=Stestcase.split("\n")


#####################################
######################################

def read_line():
  global read_line_index
  if stdin_flag:
    return raw_input()
  else:
    s=read_line_datas[read_line_index]
    read_line_index+=1
    return s

def answer():
  if stdin_flag:
    return solve()
  else:
    while read_line_proceed():
      solve()
      

def read_line_proceed():
  global read_line_index
  print"##################"
  while 1:
    if read_line_index>= len (read_line_datas ):
      return False
    if read_line_datas[read_line_index]=="%%%":
      read_line_index+=1
      return True
    read_line_index+=1


def readint():
  return int (read_line() )


def readints():
  return map(int, read_line().split(" "))

def reads():
  return read_line()

###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################

N=26
oa=ord("a")
def tn(s):return ord(s)-oa


def compute(s1,s2,dist):
  tot=0
  m=[]
  if len(s1)!=len(s2):
    print -1
    return
  for a,b in zip(s1,s2):
    k,c=dist[a][b]
    if k==-1:
      print -1
      return
    tot+=k
    m.append(chr(c+oa))
  print tot
  print "".join(m)

def dist_from(x,cmap):
  m=[-1 for i in xrange(N)]
  m[x]=0
  while 1:
    c=0
    for a in xrange(N):
      if m[a]==-1:continue
      for b in xrange(N):
        if cmap[a][b]==-1:continue
        if m[b]==-1 or m[a]+cmap[a][b]<m[b]:
          c=1
          m[b]=m[a]+cmap[a][b]
    if c==0:
      return m

def dist_n(cmap):
  res=[[-1 for x in xrange(N)] for y in xrange(N)]
  map2=dist_froms(cmap)
  for a in xrange(N):
    for b in xrange(N):
      kk=-1
      nc=-1
      for c in xrange(N):
        if map2[a][c]==-1 or map2[b][c]==-1:continue
        if kk==-1 or map2[a][c]+map2[b][c]<kk:
          kk=map2[a][c]+map2[b][c]
          nc=c
      res[a][b]=(kk,nc)

  return res

def dist_froms(cmap):
  return [dist_from(x,cmap) for x in xrange(N)]

def solve():
  s1=map(tn, reads() )
  s2=map(tn, reads() )
  n=readint()
  cmap=[[-1 for x in xrange(N)] for y in xrange(N)]
  for i in xrange(n):
    a,b,k=reads().split(" ")
    a=tn(a)
    b=tn(b)
    if int(k)<cmap[a][b] or cmap[a][b]==-1:
      cmap[a][b]=int(k)
  
  compute(s1,s2, dist_n(cmap))
  #print dist_from(tn("a"),cmap)
  #print dist_froms(cmap)[tn("a")][tn("y")]

def test():
  pass

test()
answer()