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.*;
import java.util.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class Main {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String _token() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int _int() throws IOException {
        return Integer.parseInt(_token());
    }

    long _long() throws IOException {
        return Long.parseLong(_token());
    }

    double _double() throws IOException {
        return Double.parseDouble(_token());
    }

    void dbg(Object o) {
        System.out.println(o);
    }

    void debug(Object... os) {
        System.err.println(deepToString(os));
    }    

    void run() throws IOException {
        Locale.setDefault(Locale.US);
        //String name = "a";
        //String name = "a";
        //in = new BufferedReader(new FileReader(name + ".in"));
        in = new BufferedReader(new InputStreamReader(System.in));
        //out = new PrintWriter(new File(name + ".out"));
        out = new PrintWriter(System.out);
        st = new StringTokenizer(" ");        
        int n = _int();
        int[] a = new int[n];
        array(a);
        int[] forw = new int[n + 1], back = new int[n + 1];
        forw[0] = back[0] = 0;
        for (int i = 1; i <= n; i++) {
            forw[i] = forw[i - 1] + a[i - 1];
            back[i] = back[i - 1] + a[n - i];
        }
        print(forw);
        print(back);
        out.close();
    }

    void array(int[] a) throws IOException {
        for (int i = 0; i < a.length; i++) {
            a[i] = _int();            
        }
    }

    void print(int[] a) throws IOException {
        for (int i = 0; i < a.length; i++) {
            out.print(a[i] + " ");
        }
        out.println();
    }

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    class Timer {
        long t;
        Timer() {
            t = System.currentTimeMillis();
        }
        double elapsed() {
            return (System.currentTimeMillis() - t) / 1000.;
        }
    }
}*/

import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class Main {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String _token() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int _int() throws IOException {
        return Integer.parseInt(_token());
    }

    long _long() throws IOException {
        return Long.parseLong(_token());
    }

    double _double() throws IOException {
        return Double.parseDouble(_token());
    }

    void dbg(Object o) {
        System.out.println(o);
    }

    void debug(Object... os) {
        System.err.println(deepToString(os));
    }

    void run() throws IOException {
        Locale.setDefault(Locale.US);
        //String name = "a";
        //String name = "a";
        //in = new BufferedReader(new FileReader(name + ".in"));
        in = new BufferedReader(new InputStreamReader(System.in));
        //out = new PrintWriter(new File(name + ".out"));
        out = new PrintWriter(System.out);
        st = new StringTokenizer(" ");
        String s = _token(), t = _token();
        if (s.length() != t.length()) {
            out.println(-1);
        } else {
            int n = _int();
            final int inf = Integer.MAX_VALUE / 3;
            int[][] c = new int[26][26];
            for (int i = 0; i < 26; i++)
                fill(c[i], inf);
            int w;
            String fir, sec;
            for (int i = 0; i < n; i++) {
                fir = _token();
                sec = _token();
                w = _int();
                int ind1 = fir.charAt(0) - 'a', ind2 = sec.charAt(0) - 'a';
                c[ind1][ind2] = min(c[ind1][ind2], w);
            }
            for (int i = 0; i < 26; i++)
                c[i][i] = 0;
            for (int k = 0; k < 26; k++) {
                for (int i = 0; i < 26; i++) {
                    for (int j = 0; j < 26; j++) {
                        c[i][j] = min(c[i][k] + c[k][j], c[i][j]);
                    }
                }
            }
            long ret = 0;
            char[] ans = new char[s.length()];
            boolean ok = true;
            for (int i = 0; i < s.length(); i++) {
                char ch1 = s.charAt(i), ch2 = t.charAt(i);
                int c1 = (int)(ch1 - 'a'), c2 = (int)(ch2 - 'a');
                if (c1 != c2) {
                    int best = inf;
                    char cur = '0';
                    for (int j = 0; j < 26; j++) {
                        if (c[c1][j] != inf && c[c2][j] != inf) {
                            if (best > c[c1][j] + c[c2][j]) {
                                best = c[c1][j] + c[c2][j];
                                cur = (char)('a' + j);
                            }
                        }
                    }
                    if (cur == '0') {
                        ok = false;
                        break;
                    } else {
                        ans[i] = cur;
                        ret += best;
                    }
                } else {
                    ans[i] = ch1;
                }
            }
            if (ok) {
                out.println(ret);
                out.println(new String(ans));
            } else {
                out.println(-1);
            }
        }
        out.close();
    }

    void array(int[] a) throws IOException {
        for (int i = 0; i < a.length; i++) {
            a[i] = _int();
        }
    }

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    class Timer {
        long t;
        Timer() {
            t = System.currentTimeMillis();
        }
        double elapsed() {
            return (System.currentTimeMillis() - t) / 1000.;
        }
    }
}