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 PYTHON3):

Solution:
import os
import sys
from math import *
from collections import *
# from fractions import *
# from heapq import*
from bisect import *
from io import BytesIO, IOBase
def vsInput():
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz/'
M=1000000007
EPS=1e-6
def Ceil(a,b): return a//b+int(a%b>0)
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input() 
def arrayS():return [i for i in input().split()]
 
 
#-------------------------code---------------------------#

s=list(input())
t=list(input())
n=int(input())
dic={}
adj=[[] for x in range(26)]
for x in range(n):
    aa,bb,cost=input().split(" ")
    cost=int(cost)
    a=ord(aa)-ord('a')
    b=ord(bb)-ord('a')
    chk=False
    for y in range(len(adj[a])):
        if adj[a][y][0]==b:
            adj[a][y][1]=min(cost,adj[a][y][1]) 
            chk=True
    if chk==False:
        adj[a].append([b,cost])
ans=[]
vis=[False for x in range(26)]
def dfs(x,start,cnt):
    temp=chr(start+97)+chr(x+97)
    if temp in dic.keys() and cnt>=dic[temp]:
            return
    else:
        vis[x]=True
        temp=chr(start+97)+chr(x+97)
        if temp in dic.keys():
            dic[temp]=min(dic[temp],cnt)
        else:
            dic[temp]=cnt
        for z in adj[x]:
            dfs(z[0],start,cnt+z[1])
for x in range(26):
    adj[x].sort(key=lambda x:x[1])
for x in range(26):
    dfs(x,x,0)
for x in range(ord('a'),ord('z')+1):
    temp=chr(x)+chr(x)
    if temp in dic.keys():
        dic[temp]=0
    else:
        dic[temp]=0
chk=1
spend=0
if len(s)!=len(t):
    print(-1)
else:
    for x in range(len(s)):
        if s[x]==t[x]:
            ans.append(s[x])
        else:
            notfound=True
            spendhere=int(1e100)
            thechar='a'
            for y in range(ord('a'),ord('z')+1):
                temp=s[x]+chr(y)
                retemp=t[x]+chr(y)
                if temp in dic.keys() and retemp in dic.keys():
                    notfound=False
                    if spendhere>dic[temp]+dic[retemp]:
                        thechar=chr(y)
                        spendhere=dic[temp]+dic[retemp]
            if notfound:
                chk=0
                break
            else:
                ans.append(thechar)
                spend+=spendhere
    if chk==0:
        print(-1)
    else:
        print(spend)
        print(*ans,sep="")