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

Solution:
import java.awt.Point;
import java.util.Arrays;
import java.util.Scanner;


public class p33b {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] s = in.next().toCharArray();
        char[] t = in.next().toCharArray();
        
        if(s.length != t.length) {
            System.out.println(-1);
            return;
        }
        int n = in.nextInt();
        
        int[][] all = new int[26][26];
        for (int i = 0; i < all.length; i++) {
            Arrays.fill(all[i], 999999);
        }
        for (int i = 0; i < all.length; i++) {
            all[i][i] = 0;
        }
        for (int i = 0; i < n; i++) {
            char a = in.next().charAt(0);
            char b = in.next().charAt(0);
            int c = in.nextInt();
            
            all[a-'a'][b-'a'] = Math.min(c, all[a-'a'][b-'a']);
        }
        // floyd warshall
        
        for (int k = 0; k < all.length; k++) {
            for (int i = 0; i < all.length; i++) {
                for (int j = 0; j < all.length; j++) {
                    all[i][j] = Math.min( all[i][j], all[i][k]+all[k][j] );
                }
            }
        }
        //
        
        int res = 0;
        for (int i = 0; i < s.length; i++) {
            if(s[i] == t[i])
                continue;
            int min = 999999;
            
            char a = s[i];
            char b = t[i];
            
            for (int j = 0; j < 26; j++) {
                if(all[a-'a'][j]+ all[b-'a'][j] < min) {
                    min = all[a-'a'][j]+ all[b-'a'][j];
                    //System.out.println(all[a-'a'][j]+" j  "+all[b- 'a'][j]+"  "+j);
                    s[i] = t[i] = (char)('a'+j);
                }
            }
            if(min >= 999999) {
                System.out.println(-1);
                return;
            }
            //System.out.println(min+"   i  "+i);
            res += min;
        }
        System.out.println(res);
        for (int i = 0; i < s.length; i++) {
            System.out.print(s[i]);
        }
    }
}
class change implements Comparable<change> {
    char a , b ;int  c;
    public change(char x , char y , int z) {
        a = x;
        b = y;
        c = z;
    }
    public int compareTo(change o) {
        return c - o.c;
    }
}

/*
wye
upt
13
z z 5
e t 8
t f 2
f e 3
p l 16
l s 6
s q 13
y o 4
o q 0
u w 5
k m 14
m i 10
w u 12
*/