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 main;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {

        Solve solve = new Solve(new ConsoleIOFast());
        solve.getSolve();
    }
}

interface TypeIO {

    void solve() throws IOException;
}

class FileIO implements TypeIO {

    @Override
    public void solve() throws IOException {
        Scanner sc = new Scanner(new File("input.txt"));
        PrintWriter pw = new PrintWriter(new File("output.txt"));
        sc.close();
        pw.close();
    }
}

class ConsoleIOFast implements TypeIO {

    BufferedReader in;
    StringTokenizer tok;
    PrintWriter out;

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

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

    String readString() throws IOException {
        return readToken();
    }

    @Override
    public void solve() throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        int ans = 0;
        String a = readString();
        String b = readString();
        Graph graph = new Graph(26);
        int n = readInt();
        for (int i = 0; i < n; i++) {
            String s1 = readString();
            String s2 = readString();
            String s3 = readString();
            int first = (int)(s1.charAt(0) - 'a');
            int second = (int)(s2.charAt(0) - 'a');
            int value = Integer.parseInt(s3);
            graph.addEdgeOriented(first, second, value);
        }
        if(a.length() == b.length()){
            boolean bool = true;
            StringBuffer sb = new StringBuffer();
            graph.floid();
            for(int i = 0; i < a.length(); i++){
                int res = Integer.MAX_VALUE;
                int ch = -1;
                int pos1 = a.charAt(i) - 'a';
                int pos2 = b.charAt(i) - 'a';
                for(int j = 0; j < 26; j++){
                    if(graph.dist[pos1][j] != Integer.MAX_VALUE && graph.dist[pos2][j] != Integer.MAX_VALUE && graph.dist[pos1][j] + graph.dist[pos2][j] < res){
                        res = graph.dist[pos1][j] + graph.dist[pos2][j];
                        ch = j;
                    }
                }
                if(res == Integer.MAX_VALUE){
                    bool = false;
                    break;
                }
                ans += res;
                sb.append((char)(ch + 'a'));
            }
            if(bool){
                out.println(ans);
                out.println(sb);
            }else{
                out.println(-1);
            }
        }else{
            out.println(-1);
        }
        out.close();
        in.close();
    }
}

class ConsoleIO implements TypeIO {

    @Override
    public void solve() throws IOException {
        Scanner sc = new Scanner(System.in);

        sc.close();
    }
}

class Solve {

    private TypeIO typeIO;

    public Solve(TypeIO typeIO) {
        this.typeIO = typeIO;
    }

    void getSolve() throws IOException {
        typeIO.solve();
    }
}
class Graph {
    
    public int dist[][];

    public Graph(int n) {
        dist = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                dist[i][j] = Integer.MAX_VALUE;
                if(i == j) dist[i][j] = 0;
            }
        }
    }
    
    public void floid(){
        for (int k = 0; k < dist.length; k++) {
            for (int i = 0; i < dist.length; i++) {
                for (int j = 0; j < dist.length; j++) {
                    if (dist[i][k] < Integer.MAX_VALUE && dist[k][j] < Integer.MAX_VALUE) {
                        dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
                    }
                }
            }
        }
    }
    public void addEdgeOriented(int first, int second, int value) {
        if(dist[first][second] > value){
            dist[first][second] = value;
        }
    }
}