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

public class StringProblem {
    static class Pair implements Comparable<Pair> {
        char letter;
        int cost;

        Pair(char letter, int cost) {
            this.letter = letter;
            this.cost = cost;
        }

        public int compareTo(Pair other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    private final List<Pair>[] adjacencyList;
    private int totalCost = 0;
    private String meet;

    private StringProblem() {
        adjacencyList = (ArrayList<Pair>[]) new ArrayList[26]; // cost edges coming out of every letter
        for (int i = 0; i < 26; i++) adjacencyList[i] = new ArrayList<>();

        Scanner in = new Scanner(System.in, "UTF-8");
        String first = in.next(), second = in.next();

        // If the length of the strings are different, they can't be transformed to the same word.
        if (first.length() != second.length()) return;
        final int wordLength = first.length();

        for (int i = in.nextInt(); i > 0; i--) {
            char u = in.next().charAt(0), v = in.next().charAt(0);
            int cost = in.nextInt();
            adjacencyList[u - 'a'].add(new Pair(v, cost)); // cost of changing u to v
        }

        // Don't calculate the same thing for the second time.
        Pair[][] reply = new Pair[26][26];

        StringBuilder answer = new StringBuilder(); // where the two words meet
        for (int i = 0; i < wordLength; i++) {
            int a = first.charAt(i) - 'a', b = second.charAt(i) - 'a';
            if (reply[a][b] == null) {
                Pair aToB = change(a, b); // where a and be meets and for what cost

                // Impossible.
                if (aToB == null) return;

                char meetingLetter = aToB.letter;
                int costOfChange = aToB.cost;

                totalCost += costOfChange;
                answer.append(meetingLetter);
                reply[a][b] = aToB;
            } else {
                totalCost += reply[a][b].cost;
                answer.append(reply[a][b].letter);
            }
        }
        meet = answer.toString();
    }

    private Pair change(int a, int b) {
        int[] distToA = shortestPath(a), distToB = shortestPath(b);
        int cost = Integer.MAX_VALUE, letter = -1;
        for (int i = 0; i < 26; i++) {
            if (distToA[i] != Integer.MAX_VALUE && distToB[i] != Integer.MAX_VALUE) {
                if (distToA[i] + distToB[i] < cost) {
                    cost = distToA[i] + distToB[i];
                    letter = i;
                }
            }
        }
        return letter == -1 ? null : new Pair((char) (letter + 'a'), cost);
    }

    private int[] shortestPath(int u) {
        boolean[] marked = new boolean[26];
        int[] costTo = new int[26]; // cost of change from u
        Arrays.fill(costTo, Integer.MAX_VALUE);
        costTo[u] = 0;

        PriorityQueue<Pair> distance = new PriorityQueue<>();
        distance.offer(new Pair((char) (u + 'a'), costTo[u]));

        while (!distance.isEmpty()) {
            int v = distance.poll().letter - 'a';
            marked[v] = true;
            for (Pair change : adjacencyList[v]) {
                int w = change.letter - 'a', cost = change.cost;
                if (!marked[w] && costTo[v] + cost < costTo[w]) {
                    costTo[w] = costTo[v] + cost;
                    distance.offer(new Pair((char) (w + 'a'), costTo[w]));
                }
            }
        }
        return costTo;
    }
    

    private String solve() {
        return meet == null ? "-1" : totalCost + "\n" + meet;
    }

    public static void main(String[] args) {
        System.out.println(new StringProblem().solve());
    }
}