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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args){
        FastIO io = new FastIO();
        String str1 = io.nextLine();
        String str2 = io.nextLine();
        if(str1.length() == str2.length()){
            int changes = io.nextInt();
            Graph graph = new Graph();
            for(int i = 0; i<changes; i++){
                String aux = io.nextLine();
                char a = aux.charAt(0);
                char b = aux.charAt(2);
                int cost = Integer.parseInt(aux.substring(4));
                graph.setEdge(a, b, cost);
            }
            graph.floyd();
            StrInt minimal = graph.findMin(str1, str2);
            io.append(minimal.intgr);
            if(minimal.intgr != -1){
                io.append("\n");
                io.append(minimal.str);
            }
        }else{
            io.append("-1");
        }
        io.print();
    }
}

class Graph{
    public int graph[][];
    public int floydGraph[][];
    public int alphabet;

    public Graph(){
        this.alphabet = 26;
        this.graph = new int[alphabet][alphabet];
        for(int i = 0; i<alphabet; i++){
            for(int k = 0; k<alphabet; k++){
                this.graph[i][k] = -1;
            }
        }
        this.floydGraph = new int[alphabet][alphabet];
        for(int i = 0; i<alphabet; i++){
            for(int k = 0; k<alphabet; k++){
                this.floydGraph[i][k] = -1;
            }
        }
    }

    public void setEdge(int a, int b, int cost){
        if((this.graph[a-97][b-97] == -1)||(this.graph[a-97][b-97] > cost)) {
            this.graph[a - 97][b - 97] = cost;
            this.floydGraph[a - 97][b - 97] = cost;
        }
    }

    public void floyd(){
        for(int i = 0; i<alphabet; i++){
            floyd(i);
        }
    }

    public void floyd(int i){
        int f1, f2;
        for(int j = 0; j<alphabet; j++){
            for(int k = 0; k<alphabet; k++){
                if( (j != i) && (k != i) && (j != k)){
                    f1 = this.floydGraph[j][i];
                    f2 = this.floydGraph[i][k];
                    if(( (this.floydGraph[j][k] > f1 + f2) || (this.floydGraph[j][k] == -1) ) && ( (f1 != -1) && (f2 != -1) )){
                        floydGraph[j][k] = f1 + f2;
                    }
                }
            }
        }

    }

    public StrInt findMin(String str1, String str2){
        int index;
        int totalCost = 0;
        int[] char_and_cost;
        boolean impossible = false;
        StringBuilder strF = new StringBuilder();
        int size = str1.length();
        for(int c = 0; c<size; c++){
            if( str1.charAt(c) != str2.charAt(c) ){
                    char_and_cost = getCost(str1.charAt(c), str2.charAt(c));
                    strF.append((char)char_and_cost[0]);
                    if(char_and_cost[1] != -1)
                        totalCost += char_and_cost[1];
                    else {
                        impossible = true;
                        break;
                    }
            }else{
                strF.append(str1.charAt(c));
            }
        }
        if(impossible){
            return new StrInt("", -1);
        }else {
            return new StrInt(strF.toString(), totalCost);
        }
    }

    public int[] getCost(char st, char ed){
        st -= 97;
        ed -= 97;
        int p1 = st;
        int p2 = ed;
        int alternativeCost, directCost, middleChar;
        if((this.floydGraph[p2][p1] == -1) || ((this.floydGraph[p1][p2] <= this.floydGraph[p2][p1]) && (this.floydGraph[p1][p2] != -1))){
            directCost = this.floydGraph[p1][p2];
            middleChar = p2;
        }else{
            directCost = this.floydGraph[p2][p1];
            middleChar = p1;
        }
        int finalCost = directCost;
        //Ve se tem custo menor que o direct
        for(int i = 0; i<alphabet; i++){
            if(this.floydGraph[p1][i] != -1 && this.floydGraph[p2][i] != -1){
                alternativeCost = floydGraph[p1][i] + floydGraph[p2][i];
                if((finalCost == -1)||(alternativeCost<finalCost)){
                    finalCost = alternativeCost;
                    middleChar = i;
                }
            }
        }
        int r[] = new int[2];
        r[0] = middleChar+97;
        r[1] = finalCost;
        return r;
    }
}

class StrInt{
    public String str;
    public int intgr;

    public StrInt(String str, int intgr){
        this.str = str;
        this.intgr = intgr;
    }
}

class FastIO{
    BufferedReader br;
    StringTokenizer st;
    StringBuilder buffer;
    public FastIO() {
        br = new BufferedReader(new InputStreamReader(System.in));
        buffer = new StringBuilder();
    }
    void setBuffer(StringBuilder buffer){
        this.buffer = buffer;
    }
    void clearBuffer(){
        this.buffer.setLength(0);
    }
    void print(){
        System.out.print(buffer);
    }
    void append(Object element){
        this.buffer.append(element);
    }
    void println(){
        System.out.println(buffer);
    }
    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 nextLine() {
        String str = "";
        try {
            str = br.readLine();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
    boolean hasMore() {
        return st!=null && st.hasMoreElements();
    }
}
//FAST_IO