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:
//package codeforces.ru.task_33B;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created by myduomilia on 15.02.15.
 */
public class Main {
    public static void main(String[] args) throws IOException {
        FastReadWriteConsole console = new FastReadWriteConsole();
        String a = console.nextString();
        String b = console.nextString();
        int n = console.nextInt();

        int arr[][] = new int[26][26];
        for(int i = 0; i < 26; i++){
            for(int j = 0; j < 26; j++){
                if(i == j)
                    arr[i][j] = 0;
                else
                    arr[i][j] = Integer.MAX_VALUE;
            }
        }

        for(int i = 0; i < n; i++) {
            int from = console.nextChar() - 'a';
            int to = console.nextChar() - 'a';
            int value = console.nextInt();
            arr[from][to] = Math.min(arr[from][to], value);
        }
        int sum = 0;
        StringBuilder sb = new StringBuilder();
        if(a.length() != b.length()){
            console.print(-1);
            console.close();
            return;
        }
        Floid alg = new Floid();
        double dist [][] = alg.floid(arr);
        for(int i = 0; i < a.length(); i++){
            if(a.charAt(i)!= b.charAt(i)){
                int minValue = Integer.MAX_VALUE;
                int index = -1;
                for(int j = 0; j < 26; j++){
                    if(dist[a.charAt(i) - 'a'][j] != Integer.MAX_VALUE && dist[b.charAt(i) - 'a'][j] != Integer.MAX_VALUE){
                        if(dist[a.charAt(i) - 'a'][j] + dist[b.charAt(i) - 'a'][j] < minValue){
                            minValue = (int) (dist[a.charAt(i) - 'a'][j] + dist[b.charAt(i) - 'a'][j]);
                            index = j;
                        }
                    }
                }
                if(index == -1){
                    console.print(-1);
                    console.close();
                    return;
                }else {
                    sb.append((char)(index + 'a'));
                    sum += minValue;
                }
            }else
                sb.append(a.charAt(i));
        }
        console.print(sum + "\n");
        console.print(sb);
        console.close();
    }
}
class Floid {

    private double a[][] = null;

    public double[][] floid(int adj[][]){
        a = new double[adj.length][adj.length];
        for(int i = 0; i < adj.length; i++){
            for(int j = 0; j < adj.length; j++){
                a[i][j] = adj[i][j];
            }
        }
        for (int k = 0; k < a.length; k++) {
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a.length; j++) {
                    a[i][j] = Math.min(a[i][j], a[i][k] + a[k][j]);
                }
            }
        }
        return a;
    }

}
class FastReadWriteConsole {

    BufferedReader in;
    StringTokenizer tok;
    PrintWriter out;

    public FastReadWriteConsole() {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
    }

    private String readToken() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

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

    public int nextChar() throws IOException {
        return readToken().charAt(0);
    }

    public String nextString() throws IOException {
        return readToken();
    }

    public void print(Object obj){
        out.print(obj.toString());
    }

    public void close() throws IOException {
        in.close();
        out.close();
    }

}