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.io.*;
import java.util.*;
public class infracomp2pp{
    static class Graph {
        private int[][] matrix;
        public Graph(){
            this.matrix = new int[26][26];
            for (int i = 0; i < 26; i++){
                for (int j = 0; j < 26; j++)
                    this.matrix[i][j] = 5555;
            }
        }
        public void setEdge(int v1, int v2, int weight){
            if (this.matrix[v1-97][v2-97] > weight)
                this.matrix[v1-97][v2-97] = weight;
        }
        public int[][] floyd(){
            for (int i = 0; i < 26; i++)
                for (int j = 0; j < 26; j++)
                    for (int k = 0; k < 26; k++)
                        if (this.matrix[j][k] > this.matrix[j][i] + this.matrix[i][k]) this.matrix[j][k] = this.matrix[j][i] + this.matrix[i][k];
            return this.matrix;
        }
    }
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        public String next(){
            while(st == null || !st.hasMoreElements()){
                try {
                    st = new StringTokenizer(br.readLine());
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public String nextLine(){
            String str = "";

            try {
                str = br.readLine();
            }catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
        FastReader fr = new FastReader();
        Graph graph = new Graph();
        int count = 0;
        StringBuilder output = new StringBuilder();
        String aux1 = fr.nextLine(),aux2 = fr.nextLine();
        if (aux1.length() != aux2.length())count = -1;
        else {
            for (int i = fr.nextInt(); i > 0; i--) {
                String v1 = fr.next(),v2 = fr.next();
                int weight = fr.nextInt();
                graph.setEdge(v1.charAt(0), v2.charAt(0), weight);
            }
            int[][] matrix = graph.floyd();
            for (int i = 0; i < aux1.length(); i++){
                int v1 = aux1.charAt(i) - 97;
                int v2 = aux2.charAt(i) - 97;
                int cost = matrix[v1][v2];
                char newl = (char) (v2 + 97);
                if (v1 != v2) {
                    if (matrix[v2][v1] < cost) {
                        cost = matrix[v2][v1];
                        newl = (char) (v1 + 97);
                    }
                    for (int j = 0; j < 26; j++) {
                        if (matrix[v2][j] != 5555 && matrix[v1][j] != 5555)
                            if (matrix[v2][j] + matrix[v1][j] < cost) {
                                cost = matrix[v2][j] + matrix[v1][j];
                                newl = (char)(j + 97);
                            }
                    }
                    if (cost == 5555) {
                        count = -1;
                        i = aux1.length();
                    }
                    else {
                        count += cost;
                        output.append(newl);
                    }
                }
                else output.append(newl);
            }
        }
        System.out.println(count);
        if (count != -1) System.out.println(output.toString());
    }
}