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.util.Arrays.fill;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;

public class Main {
	public static void main(String[] args) throws IOException {
		try {
			if (new File("input.txt").exists())
				System.setIn(new FileInputStream("input.txt"));
		} catch (SecurityException e) {}
		
		new Main().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st = new StringTokenizer("");
	
	boolean[][] g = new boolean [256][256];
	long[][] c = new long [256][256];
	
	void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		
		char[] s1 = nextToken().toCharArray();
		char[] s2 = nextToken().toCharArray();
		
		int n = nextInt();
		
		for (int i = 0; i < n; i++) {
			char c1 = nextToken().charAt(0);
			char c2 = nextToken().charAt(0);
			if (g[c1][c2])
				c[c1][c2] = min(c[c1][c2], nextInt());
			else
				c[c1][c2] = nextInt();
			g[c1][c2] = true;
		}
		
		for (int i = 0; i < 256; i++)
			for (int j = 0; j < 256; j++) {
				g[i][i] = true;
				c[i][i] = 0;
			}
		
		for (int k = 'a'; k <= 'z'; k++)
			for (int i = 'a'; i <= 'z'; i++)
				for (int j = 'a'; j <= 'z'; j++) {
					if (g[i][k] && g[k][j] && (!g[i][j] || c[i][j] > c[i][k] + c[k][j])) {
						g[i][j] = true;
						c[i][j] = c[i][k] + c[k][j];
					}
				}
		
		if (s1.length != s2.length) {
			out.println(-1);
			out.close();
			return;
		}
		
		long ans = 0;
		boolean ok = true;
		
		for (int i = 0; i < s1.length; i++) {
			if (s1[i] != s2[i]) {
				ok = false;
				long cost = Long.MAX_VALUE;
				char bc = '.';
				for (int sc1 = 'a'; sc1 <= 'z'; sc1++)
					for (int sc2 = 'a'; sc2 <= 'z'; sc2++) {
						if (g[s1[i]][sc1] && g[s2[i]][sc2] && g[sc1][sc2]) {
							ok = true;
							long nc = c[s1[i]][sc1] + c[s2[i]][sc2] + c[sc1][sc2];
							if (cost > nc) {
								cost = nc;
								bc = (char) sc2;
							}
						}
					}
				if (!ok)
					break;
				ans += cost;
				s1[i] = bc;
			}
		}
		
		if (!ok) {
			out.println(-1);
			out.close();
			return;
		}
		
		out.println(ans);
		for (int i = 0; i < s1.length; i++)
			out.print(s1[i]);	
		out.println();
		out.close();
	}
	
	String nextToken() throws IOException {
		while (!st.hasMoreTokens())
			st = new StringTokenizer(in.readLine());
		return st.nextToken();
	}
	
	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}
	
	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}
	
	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
	
	String nextLine() throws IOException {
		st = new StringTokenizer("");
		return in.readLine();
	}
	
	boolean EOF() throws IOException {
		while (!st.hasMoreTokens()) {
			String s = in.readLine();
			if (s == null)
				return true;
			st = new StringTokenizer(s);
		}
		return false;
	}
}