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:
/*******************************************************************
My love has gone away,
quietly after a hundered days.
This is what's she has always said she won't stay
for more than what she can repay.

I can still hear her say there
that I'm not hearing tender play
The day she let me kiss her was a display,
of love to those who she betray.

How can I put someone to the test,
when I thought I got the best.
Untill the taste of bitterness then I regret
but still that I won't detest,
the love I can't forget,
like someone who has left.

How can i leave someone for the rest
when i'm alone facing the best
Untill they take some try to reason i regret
But still that i won't detest
who i can never forget
like someone i once met .. 
 ******************************************************************/
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.InputMismatchException;

public class TaskB_33 {

	static final int INF = Integer.MAX_VALUE / 2;

	public static void main(String[] args) {
		input();
		solve();
	}

	static void solve() {
		if (s1.length != s2.length) {
			System.out.println(-1);
			return;
		}
		int length = s1.length;
		int cost = 0;
		StringBuilder res = new StringBuilder();

		// PloydWarshall
		for (int k = 97; k < 123; k++) {
			m[k][k] = 0;
			for (int i = 97; i < 123; i++) {
				if (m[i][k] == INF) {
					continue;
				}
				for (int j = 97; j < 123; j++) {

					if (m[k][j] == INF)
						continue;
					m[i][j] = Math.min(m[i][k] + m[k][j], m[i][j]);
				}
			}
		}
		int min;
		int pos = 0;
		for (int i = 0; i < length; i++) {
			if (s1[i] != s2[i]) {
				min = INF;
				for (int j = 97; j < 123; j++) {
					if (m[s1[i]][j] + m[s2[i]][j] < min) {
						min = m[s1[i]][j] + m[s2[i]][j];
						pos = j;
					}
				}
				res.append((char) pos);
				if (min == INF) {
					System.out.println(-1);
					return;
				}
				cost += min;
			} else {
				res.append(s1[i]);
			}
		}
		System.out.println(cost);
		System.out.println(res);
	}

	/************************************************************************************/
	static char[] s1;
	static char[] s2;
	static int n;
	static int[][] m = new int[123][123];

	static void input() {
		s1 = nextString().toCharArray();
		s2 = nextString().toCharArray();
		n = nextInt();
		for (int i = 0; i < 123; i++) {
			Arrays.fill(m[i], INF);
		}

		char a;
		char b;
		int w;
		for (int i = 0; i < n; i++) {
			a = nextChar();
			b = nextChar();
			w = nextInt();
			m[a][b] = Math.min(m[a][b], w);
		}

	}

	/************************************************************************************/
	static InputStream is = System.in;
	static private byte[] buffer = new byte[1024];
	static private int lenbuf = 0, ptrbuf = 0;

	static private int readByte() {
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(buffer);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return buffer[ptrbuf++];
	}

	static private boolean isSpace(int c) {
		return !(c >= 33 && c <= 126);
	}

	static private int read() {
		int b;
		while ((b = readByte()) != -1 && isSpace(b))
			;
		return b;
	}

	static private double nextDouble() {
		return Double.parseDouble(nextString());
	}

	static private char nextChar() {
		return (char) read();
	}

	static private String nextString() {
		int b = read();
		StringBuilder sb = new StringBuilder();
		while (!(isSpace(b))) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	static private char[] nextString(int n) {
		char[] buf = new char[n];
		int b = read(), p = 0;
		while (p < n && !(isSpace(b))) {
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	static private int nextInt() {
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nextLong() {
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	static private int[] nextAi(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}
}