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.util.*;
import java.io.*;

public class B33 {
    public static void main(String[] args) throws IOException {
        PrintWriter w = new PrintWriter(System.out);
        InputReader in = new InputReader(System.in);
        String a = in.nextString();
        String b = in.nextString();
        int n = in.nextInt();
        int[][] matrix = new int[26][26];
        for (int i=0; i<26; i++) {
            Arrays.fill(matrix[i], Integer.MAX_VALUE);
        }
        for (int i=0; i<n; i++) {
            String from = in.nextString();
            String to = in.nextString();
            int cost = in.nextInt();
            matrix[from.charAt(0)-97][to.charAt(0)-97] = Math.min(matrix[from.charAt(0)-97][to.charAt(0)-97], cost);
        }
        for (int k=0; k<26; k++) {
            for (int i=0; i<26; i++) {
                for (int j=0; j<26; j++) {
                    if (matrix[i][k] < Integer.MAX_VALUE && matrix[k][j] < Integer.MAX_VALUE) {
                        matrix[i][j] = Math.min(matrix[i][j], matrix[i][k] + matrix[k][j]);
                    }
                }
            }
        }
        for (int i=0; i<26; i++) matrix[i][i] = 0;
        // for (int i=0; i<26; i++) {
        //     for (int j=0; j<26; j++) {
        //         if (matrix[i][j] != Integer.MAX_VALUE) {
        //             w.println(i + " " + j + " " + matrix[i][j]);
        //         }
        //     }
        //     w.println();
        // }
        int cost = 0;
        if (a.length() != b.length()) {
            w.println(-1);
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i=0; i<a.length(); i++) {
                if (a.charAt(i) != b.charAt(i)) {
                    int cc = Integer.MAX_VALUE;
                    char x = 'p';
                    for (int j=0; j<26; j++) {
                        if (matrix[a.charAt(i)-97][j] < Integer.MAX_VALUE && matrix[b.charAt(i)-97][j] < Integer.MAX_VALUE && (matrix[a.charAt(i)-97][j] + matrix[b.charAt(i)-97][j] < cc)) {
                            cc = matrix[a.charAt(i)-97][j] + matrix[b.charAt(i)-97][j];
                            x = (char)(j+97);
                        }
                    }
                    if (cc == Integer.MAX_VALUE) {
                        cost = -1;
                        break;
                    } else {
                        sb.append(x);
                        cost += cc;
                    }
                } else {
                    sb.append(a.charAt(i));
                }
            }
            w.println(cost);
            if (cost != -1) {
                w.println(sb.toString());
            }
        }
        w.close();
    }
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
	
        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

}