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.PrintWriter;
import java.util.Scanner;

public class B33 {

    /**
     * @param args
     */
    private int m;
    private int n;
    private int a[][];
    private int dist[][];
    private boolean was[][];
    private String first;
    private String second;

    public void solve() {
        Scanner in = new Scanner(System.in);
        PrintWriter writer = new PrintWriter(System.out);
        int n = 'z' - 'a' + 1;
        a = new int[n][n];
        dist = new int[n][n];
        was = new boolean[n][n];
        first = in.nextLine();
        second = in.nextLine();
        m = in.nextInt();
        in.nextLine();
        String x;
        String y;
        int w;
        for (int i = 0; i < m; i++) {
            x = in.next();
            y = in.next();
            w = in.nextInt();
            if (!was[x.charAt(0) - 'a'][y.charAt(0) - 'a']) {
                was[x.charAt(0) - 'a'][y.charAt(0) - 'a'] = true;
                a[x.charAt(0) - 'a'][y.charAt(0) - 'a'] = w;
            } else {
                if (a[x.charAt(0) - 'a'][y.charAt(0) - 'a'] > w) {
                    a[x.charAt(0) - 'a'][y.charAt(0) - 'a'] = w;
                }
            }
        }
        int inf = 1 << 29;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                if (!was[i][j]){
                    dist[i][j] = inf;
                }else{
                    dist[i][j] = a[i][j];
                }
            }
        for (int i = 0; i < n; i++){
            dist[i][i] = 0;
        }
        for (int k = 0; k < n; k++)
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    if (dist[i][j] > dist[i][k] + dist[k][j]){
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
        StringBuilder out = new StringBuilder();
        int minCost = 0;
        if (first.length() != second.length()){
            writer.println(-1);
            writer.flush();
            writer.close();
            return;
        }
        int minDist;
        int middle;
        for (int i = 0; i < first.length(); i++){
            if (first.charAt(i) == second.charAt(i)){
                out.append(first.charAt(i));
            }else{
                minDist = inf;
                middle = -1;
                for (int j = 0; j < n; j++){
                    if (dist[first.charAt(i) - 'a'][j] + dist[second.charAt(i)- 'a'][j] < minDist){
                        minDist = dist[first.charAt(i)- 'a'][j] + dist[second.charAt(i)- 'a'][j];
                        middle = j;
                    }
                }
                if (middle == -1){
                    writer.println(-1);
                    writer.flush();
                    writer.close();
                    return;
                }
                out.append( (char)(middle + 'a') );
                minCost += minDist;
            }
        }
        writer.println(minCost);
        writer.println(out);
        in.close();
        writer.flush();
        writer.close();
    }

    public static void main(String[] args) {
        B33 task = new B33();
        task.solve();

    }

}