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.StringTokenizer;

public class Q33B implements Runnable {
    private void solve() throws IOException {
        char[] s = nextToken().toCharArray();
        char[] t = nextToken().toCharArray();
        if (s.length != t.length) {
            writer.print(-1);
            return;
        }
        int Z = 27;
        int n = nextInt();
        int[][] tr = new int[Z][Z];
        for (int i = 0; i < Z; i++)
            for (int j = 0; j < Z; j++) {
                tr[i][j] = -1;
            }
        for (int i = 0; i < Z; i++) {
            tr[i][i] = 0;
        }
        for (int i = 0; i < n; i++) {
            int from = nextToken().charAt(0) - 'a';
            int to = nextToken().charAt(0) - 'a';
            int cost = nextInt();
            if (tr[from][to] == -1 || tr[from][to] > cost) {
                tr[from][to] = cost;
            }
        }
        for (int k = 0; k < Z; k++) {
            for (int i = 0; i < Z; i++) {
                for (int j = 0; j < Z; j++) {
                    if (tr[i][k] != -1 && tr[k][j] != -1) {
                        if (tr[i][j] == -1) {
                            tr[i][j] = tr[i][k] + tr[k][j];
                        } else {
                            if (tr[i][j] > tr[i][k] + tr[k][j]) {
                                tr[i][j] = tr[i][k] + tr[k][j];
                            }
                        }
                    }
                }
            }
        }
        int[][] tp = new int[Z][Z];

        for (int i = 0; i < Z; i++) {
            for (int j = 0; j < Z; j++) {
                if (tr[i][j] != -1) {
                    if(tr[j][i]==-1){
                        tp[i][j]=j;
                        tp[j][i]=j;
                    }else{
                        if(tr[i][j]<tr[j][i]){
                            tp[i][j]=j;tp[j][i]=j;
                        }
                    }
                } else {
                    tp[i][j] = -1;
                }
            }
        }
        for (int i = 0; i < Z; i++) tp[i][i] = i;
        for (int i = 0; i < Z; i++) {
            for (int j = 0; j < Z; j++) {
                for (int k = 0; k < Z; k++) {
                    if (tr[i][k] != -1 && tr[j][k] != -1) {
                        if(tp[i][j]==-1){
                            tp[i][j]=k;
                            tp[j][i]=k;
                        }else if (tr[i][tp[i][j]]+tr[j][tp[i][j]] > tr[i][k] + tr[j][k]) {
                            tp[i][j]=k;
                            tp[j][i]=k;
                        }
                    }
                }
            }
        }
        

        int count = 0;
        StringBuilder b = new StringBuilder();

        for (int r = 0; r < s.length; r++) {
            int i = s[r] - 'a';
            int j = t[r] - 'a';
            if(tp[i][j]==-1){
                writer.print(-1);
                return;
            }
            count += tr[i][tp[i][j]]+tr[j][tp[i][j]];
            b.append((char)(tp[i][j]+'a'));
        }
        writer.println(count);
        writer.print(b.toString().trim());
    }

    public static void main(String[] args) {
        new Q33B().run();
    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {

            boolean oj = System.getProperty("ONLINE_JUDGE") != null;

            reader = oj ? new BufferedReader(new InputStreamReader(System.in)) :
                    new BufferedReader(new FileReader(new File("input.txt")));


            tokenizer = null;
            writer = new PrintWriter(System.out);
            long start = System.currentTimeMillis();
            solve();
            reader.close();
            if (!oj) {
                writer.print("\nTime:" + (System.currentTimeMillis() - start));
            }
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }
}