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.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;
import java.io.DataInputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author karan173
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }
}

class Task
{
    int k;
    int INF =  999999999;
    public void solve(int testNumber, FastReader in, PrintWriter out)
    {

        char a[] = in.nextString().toCharArray();
        char b[] = in.nextString().toCharArray();
        if(a.length != b.length)
        {
            out.println(-1);
            return;
        }

        for(int i = 0; i<26; i++)
        {
            for(int j = 0; j<26; j++)
            {
                minDist[i][j] = INF;
            }
        }
        for(int i = 0; i<26; i++)
            minDist[i][i] = 0;
        k = in.nextInt();
        for(int i=0; i<k;i++)
        {
            int x = in.nextChar()-'a';
            int y = in.nextChar() - 'a';
            int w = in.nextInt();
            if(w<minDist[x][y])
            {
                minDist[x][y] = w;
                c[x][y] = (char) (y+'a');
            }
        }
        floyd();
        long ans = 0;
        for(int i = 0; i<a.length; i++)
        {
            if(a[i]!=b[i])
            {
                int a1 = a[i]-'a';
                int b1 = b[i] - 'a';
                if(t[a1][b1] == INF)
                {
                    out.println(-1);
                    return;
                }
                ans += t[a1][b1];
                a[i] = c[a1][b1];

//                ans += val;
//                a[i] = c[a1][b1];
//                out.println(b[i]+" changed to "+a[i]);
            }
        }
        out.println(ans);
        out.println(a);
    }
    char c[][] = new char[26][26];
    int t[][] = new int[26][26];
    private void floyd()
    {

        for(int k = 0; k<26; k++)
        {
            for(int i=0; i<26; i++)
            {
                for(int j = 0; j<26; j++)
                {
                    int z = minDist[i][k] + minDist[k][j];
                    if(z < minDist[i][j])
                    {
                       // System.out.println((char)(i+'a') + " " + (char)(j+'a') + " "+(char)(k+'a'));
                        minDist[i][j] = z;
                        //c[i][j] = (char) (k+'a');
                    }
                }
            }
        }

        //suppose we are given chars a and b. We change the 2 to c.
        //penalty = Min( mindist[a][c] + mindist[b][c]  )
        for(int k = 0; k<26; k++)
        {
            for(int i=0; i<26; i++)
            {
                int penalty = INF;
                for(int j = 0; j<26; j++)
                {
                     if(minDist[k][j] + minDist[i][j] < penalty)
                     {
                         penalty = minDist[k][j] + minDist[i][j];
                         c[k][i] = (char)(j+'a');
                     }

                }
                t[k][i] = penalty;
            }
        }
    }

    int minDist[][] = new int[26][26];

}

class FastReader
{
    final private int BUFFER_SIZE = 1 << 17;

    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public FastReader(InputStream in) {
        din = new DataInputStream(in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }
    public byte next()
    {
        try {
            return read();
        } catch (Exception e) {
            throw new InputMismatchException();
        }
    }
    public String nextString() {
        StringBuffer sb = new StringBuffer("");
        byte c = next();
        while (c <= ' ')
            c = next();
        do {
            sb.append((char) c);
            c = next();
        } while (c > ' ');
        return sb.toString();
    }

    public char nextChar() {
        byte c = next();
        while (c <= ' ')
            c = next();
        return (char) c;
    }

    public int nextInt() {
        int ret = 0;
        byte c = next();
        while (c <= ' ')
            c = next();
        boolean neg = c == '-';
        if (neg)
            c = next();
        do {
            ret = (ret << 3) + (ret << 1) + c - '0';
            c = next();
        } while (c > ' ');
        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() {
        try {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        } catch (IOException e) {
            throw new InputMismatchException();
        }
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read()  {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }
}