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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javafx.util.Pair;

import java.util.Vector;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        b33 solver = new b33();
        solver.solve(1, in, out);
        out.close();
    }

    static class b33 {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            String s = in.next();
            String t = in.next();
            int maxW = 1000000;
            if (s.length() != t.length()) {
                out.println(-1);
                return;
            }
            int[][] cost = new int[26][26];
            for (int i = 0; i < 26; i++) {
                for (int j = 0; j < 26; j++) {
                    cost[i][j] = maxW;
                }
            }
            int n = in.nextInt();
            for (int i = 0; i < n; i++) {
                int A = in.next().charAt(0) - 'a';
                int B = in.next().charAt(0) - 'a';
                int W = in.nextInt();
                cost[A][B] = Math.min(cost[A][B], W);
            }
            for (int i = 0; i < 26; i++) {
                boolean[] used = new boolean[26];
                Stack<Pair<Integer, Integer>> dfs = new Stack<>();
                dfs.add(new Pair<>(i, 0));
                used[i] = true;
                while (!dfs.isEmpty()) {
                    Pair<Integer, Integer> cur = dfs.pop();
                    cost[i][cur.getKey()] = Math.min(cost[i][cur.getKey()], cur.getValue());
                    for (int j = 0; j < 26; j++) {
                        if (!used[j] && cost[cur.getKey()][j] < maxW) {
                            used[j] = true;
                            dfs.push(new Pair<>(j, cur.getValue() + cost[cur.getKey()][j]));
                        }
                    }
                }
            }
            for (int mid = 0; mid < 26; mid++) {
                for (int i = 0; i < 26; i++) {
                    for (int j = 0; j < 26; j++) {
                        cost[i][j] = Math.min(cost[i][j], cost[i][mid] + cost[mid][j]);
                    }
                }
            }
            char[] ans = new char[s.length()];
            int ansCost = 0;
            for (int i = 0; i < s.length(); i++) {
                char best = '&';
                int curCost = maxW;
                int tChar = t.charAt(i) - 'a';
                int sChar = s.charAt(i) - 'a';
                for (int curChar = 0; curChar < 26; curChar++) {
                    int sCost = cost[sChar][curChar];
                    int tCost = cost[tChar][curChar];
                    if (sCost + tCost < curCost) {
                        curCost = sCost + tCost;
                        best = (char) (curChar + 'a');
                    }
                }
                if (best == '&') {
                    out.println(-1);
                    return;
                }
                ans[i] = best;
                ansCost += curCost;
            }
            out.println(ansCost);
            out.println(ans);
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer stt;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        public String next() {
            while (stt == null || !stt.hasMoreTokens()) {
                stt = new StringTokenizer(nextLine());
            }
            return stt.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}