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.*;

public class B {

	void run() throws IOException {
		char[] a = nl().toCharArray(), b = nl().toCharArray();
		int n = a.length, m = ni(), q = 27, inf = 123456;
		if (b.length != n) {
			pw.print(-1);
			return;
		}
		long ans = 0;
		int[][] d = new int[q][q], f = new int[q][q], g = new int[q][q];
		for (int i = 0; i < q; i++)
			for (int j = 0; j < q; j++)
				if (i == j)
					g[i][j] = j;
				else
					f[i][j] = d[i][j] = inf;
		int[] x = new int[m], y = new int[m];
		for (int i = 0; i < m; i++) {
			int u = (int) next().charAt(0) - 97;
			int v = (int) next().charAt(0) - 97;
			x[i] = u;
			y[i] = v;
			int w = ni();
			if (w < d[u][v])
				d[u][v] = w;
		}

		for (int i = 0; i < m; i++)
			for (int u = 0; u < q; u++)
				for (int v = 0; v < q; v++)
					if (d[u][x[i]] + d[x[i]][y[i]] + d[y[i]][v] < d[u][v]) {
						f[u][v] = d[u][v] = d[u][x[i]] + d[x[i]][y[i]] + d[y[i]][v];
						g[u][v] = v;
					}

		for (int i = 0; i < q; i++)
			for (int j = 0; j < q; j++)
				for (int k = 0; k < q; k++)
					if (d[i][k] + d[j][k] < f[i][j]) {
						f[i][j] = d[i][k] + d[j][k];
						g[i][j] = k;
					}

		for (int i = 0; i < n; i++) {
			int u = (int) a[i] - 97;
			int v = (int) b[i] - 97;
			if (f[u][v] < inf) {
				a[i] = (char) (g[u][v] + 97);
				ans += f[u][v];
			} else {
				pw.print(-1);
				return;
			}
		}
		pw.println(ans);
		pw.print(new String(a));
	}

	String next() throws IOException {
		while (st == null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	int ni() throws IOException {
		return Integer.parseInt(next());
	}

	String nl() throws IOException {
		return br.readLine();
	}

	PrintWriter pw;
	BufferedReader br;
	StringTokenizer st;

	public static void main(String[] args) throws IOException {
		long timeout = System.currentTimeMillis();
		boolean CF = System.getProperty("ONLINE_JUDGE") != null;
		PrintWriter _pw = new PrintWriter(System.out);
		BufferedReader _br = new BufferedReader(CF ? new InputStreamReader(System.in) : new FileReader(new File("in.txt")));
		new B(_br, _pw).run();
		if (!CF) {
			_pw.println();
			_pw.println(System.currentTimeMillis() - timeout);
		}
		_br.close();
		_pw.close();
	}

	public B(BufferedReader _br, PrintWriter _pw) {
		br = _br;
		pw = _pw;
	}
}