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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

		String A = r.readLine();
		String B = r.readLine();

		int[][] costs = new int[26][26];

		//Intialize the graph.
		for (int i = 0; i < costs.length; i++) {
			for (int j = 0; j < costs.length; j++) {
				if(i==j)costs[i][i]=0;
				else costs[i][j]=100000;
			}
		}
		
		//Build the graph.
		int n = Integer.parseInt(r.readLine());
		while (n-- > 0) {
			String[] input = r.readLine().split("[ ]+");
			int a = input[0].charAt(0)-'a';
			int b = input[1].charAt(0)-'a';
			int co = Integer.parseInt(input[2]);
			
			costs[a][b]=Math.min(co, costs[a][b]);
		}
		
		//Floyed Warshall.
		for (int k = 0; k < costs.length; k++) {
			for (int i = 0; i < costs.length; i++) {
				for (int j = 0; j < costs.length; j++) {
					if(costs[i][j]>costs[i][k]+costs[k][j]){
						costs[i][j]=costs[i][k]+costs[k][j];
					}
				}
			}
		}
		
		//Analysis the two Strings .
		long total = 0;
		if (A.length() == B.length()) {
			StringBuilder output= new StringBuilder();
			boolean valid = true;
			for (int i = 0; i < A.length() && valid ; i++) {
				
				int from  = A.charAt(i)-'a';
				int to = B.charAt(i)-'a';
				
				if (from  != to) {
					int minimum = Integer.MAX_VALUE;
					int minimumIndex = -1;
					
					for (int k = 0; k < costs.length; k++) {
						if(costs[from][k]+costs[to][k]<minimum){
							minimum = costs[from][k]+costs[to][k];
							minimumIndex = k;
						}
					}
					
					if(minimum > 1000)valid =false;
					if(valid){
						total += minimum;
						output.append((char)(minimumIndex+'a'));
					}
					
				}else{
					output.append((char)(from+'a'));
				}
			}
			if(valid)System.out.println(total+"\n"+output);
			else System.out.println(-1);
		} else {
			System.out.println(-1);
		}
	}
}