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:

# http://codeforces.com/problemset/problem/33/B
# 33b: String Problem.

# 象道, montreal, quebec, canada.

#input = raw_input

def calc_path(a):
    '''
    this function uses floyd-warshall alg to compute all pairs shortest path weight among alphabet characters.
    input:
    a: a 2d list which gives the weighted edge data. e.g., a = [['a', 'x', 42]]: only one edge, ('a', 'x'), with weight 42.
    result:
    all pairs shortest path weight matrix, a 26-length 2d list. w[i][i] == 0; for i != j, w[i][j] == infinite indicates there's no path between i and j.
    '''
    w = [[float('inf') for col in range(26)] for row in range(26)]
    for i in range(26):
        w[i][i] = 0
    for b in a:
        if w[ord(b[0]) - 97][ord(b[1]) - 97] > b[2]:
            w[ord(b[0]) - 97][ord(b[1]) - 97] = b[2]
    for k in range(26):
        for i in range(26):
            for j in range(26):
                if w[i][j] > w[i][k] + w[k][j]:
                    w[i][j] = w[i][k] + w[k][j]
    return w

def test_calc_path():
    a = [['a', 'b', 2], ['a', 'c', 6], ['b', 'm', 5], ['c', 'm', 3]]
    m = calc_path(a)
    print(m)
    print(m[ord('a') - 97][ord('m') - 97])
    print(m[ord('a') - 97][ord('b') - 97])

def transfer(s, t, a):
    '''
    this function firstly invokes calc_path to compute all pairs shortest path among alphabet characters, and then computes the minimum transformation position by position of s and t. finally, a minimal transformation weight and outcome is returned.
    input:
    s, t: the input string to be transformed.
    a: the character transformation cost list, 2d. e.g., a = [['a', 'x', 42], ['u', 'v', 8]]: a => x costs 42; u => v costs 8.
    result:
    -1 if transformation is not possible; otherwise, the minimal transformation cost and result, e.g. '12\nabc'.
    '''
    if len(s) != len(t):
        return -1
    r = ''
    z = 0
    w = calc_path(a)
    for d, p in zip(s, t):
        if d == p:
           r += d
        else:
            c = float('inf')
            q = ''
            i = ord(d) - 97
            j = ord(p) - 97
            for k in range(26):
                v = w[i][k] + w[j][k]
                if c > v:
                    c = v
                    q = chr(k + 97)
            if c == float('inf'):
                return -1
            z += c
            r += q
    r = str(z) + '\n' + r
    return r

def test_transfer():
    s = 'uayd'
    t = 'uxxd'
    a = [['a', 'x', 8], ['x', 'y', 13], ['d', 'c', 3]]
    assert transfer(s, t, a) == '21\nuxyd'
    s1 = 'a'
    t1 = 'b'
    a1 = [['a', 'b', 2], ['a', 'b', 3], ['b', 'a', 5]]
    assert transfer(s1, t1, a1) == '2\nb'
    s2 = 'abc'
    t2 = 'ab'
    a2 = [['a', 'b', 4], ['a', 'b', 7], ['b', 'a', 8], ['c', 'b', 11], ['c', 'a', 3], ['a', 'c', 0]]
    assert transfer(s2, t2, a2) == -1
    s3 = 'abcd'
    t3 = 'acer'
    a3 = [['b', 'c', 100], ['c', 'b', 10], ['c', 'x', 1], ['e', 'x', 3], ['c', 'e', 7], ['r', 'd', 11]]
    assert transfer(s3, t3, a3) == '25\nabxd'

def fun():
    s = input()
    t = input()
    n = int(input())
    a = []
    for c in range(n):
        x, y, z = map(str, input().split())
        a.append([x, y, int(z)])
    print(transfer(s, t, a))

#test_calc_path()
#test_transfer()
fun()