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.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class CodeForces {
    public static void main(String[] args) {
        FastIO io = new FastIO();

        String first = io.nextLine();
        String second = io.nextLine();

        if (first.length() != second.length()) {
            System.out.println("-1");
            return;
        }

        int numberOfEdges = io.nextInt();
        List<Edge> edges = new ArrayList<>(numberOfEdges);

        for (int edge = 0; edge < numberOfEdges; edge++) {
            char from = io.nextToken().charAt(0);
            char to = io.nextToken().charAt(0);

            int weight = io.nextInt();

            edges.add(new Edge(from, to, weight));
        }

        Map<Character, Integer> charToVertex = new HashMap<>();
        Map<Integer, Character> vertexToChar = new HashMap<>();
        int ci = 0;
        Set<Character> unique = new HashSet<>();

        for (Edge edge : edges) {
            if (unique.add(edge.from)) {
                charToVertex.put(edge.from, ci);
                vertexToChar.put(ci, edge.from);
                ci++;
            }
            if (unique.add(edge.to)) {
                charToVertex.put(edge.to, ci);
                vertexToChar.put(ci, edge.to);
                ci++;
            }
        }

        for (int i = 0; i < first.length(); i++) {
            if (first.charAt(i) == second.charAt(i)) {
                continue;
            }
            if (!unique.contains(first.charAt(i)) || !unique.contains(second.charAt(i))) {
                System.out.println("-1");
                return;
            }
        }

        int vertices = unique.size();
        int[][] dist = new int[vertices][vertices];
        for (int[] row : dist) {
            Arrays.fill(row, Integer.MAX_VALUE);
        }

        for (int i = 0; i < vertices; i++) {
            dist[i][i] = 0;
        }

        for (Edge edge : edges) {
            int cur = dist[charToVertex.get(edge.from)][charToVertex.get(edge.to)];
            if (edge.weight < cur) {
                dist[charToVertex.get(edge.from)][charToVertex.get(edge.to)] = edge.weight;
            }
        }

        for (int k = 0; k < vertices; k++) {
            for (int i = 0; i < vertices; i++) {
                for (int j = 0; j < vertices; j++) {
                    if (dist[i][j] > (long) dist[i][k] + dist[k][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }

        long minTotalPrice = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < first.length(); i++) {
            char fc = first.charAt(i);
            char sc = second.charAt(i);

            if (fc == sc) {
                sb.append(fc);
                continue;
            }

            long m = Integer.MAX_VALUE;
            int toIndex = -1;
            for (int j = 0; j < vertices; j++) {
                long candidateCost = (long) dist[charToVertex.get(fc)][j] + dist[charToVertex.get(sc)][j];

                if (candidateCost < m) {
                    m = candidateCost;
                    toIndex = j;
                }
            }

            if (toIndex == -1) {
                System.out.println("-1");
                return;
            }

            sb.append(vertexToChar.get(toIndex));

            minTotalPrice += m;
        }

        System.out.println(minTotalPrice);
        System.out.println(sb.toString());

        io.close();
    }

    static class Edge {
        char from;
        char to;
        int weight;

        public Edge(char from, char to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }
    }

    static class FastIO extends PrintWriter {
        BufferedReader br;
        StringTokenizer st;

        public FastIO() {
            super(new BufferedOutputStream(System.out));
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }
    }
}