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

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        FastReader fr = new FastReader();
        StringBuilder line1 = new StringBuilder(fr.nextLine());
        StringBuilder line2 = new StringBuilder(fr.nextLine());
        int answer = 0;
        if (line1.length() != line2.length()) {
        	System.out.println(-1);
        	//ending operation
        	return;
        }
        //populating our matrix
        for (int i = 0; i < 26; i++)
        	for (int j = 0; j < 26; j++) {
        		if (i == j) continue; //skip this iteration
        		cost[i][j] = 999;
        	}
        int operations = fr.nextInt();
        while (operations > 0) {
        	//picking the ascii char value to fill the correct position in the matrix
        	//ex: a - a = 0, b - a = 1
        	//cost[0][1] = cost to change a to b
        	String line = fr.nextLine();
        	//0 -> first letter
        	//1 -> space
        	//2 -> second letter
        	//3 -> space
        	//4 and ahead -> tradeCost
        	int char1int = line.charAt(0) - 'a';
        	int char2int = line.charAt(2) - 'a';
        	int tradeCost = Integer.parseInt(line.substring(4, line.length()));
        	//now we'll trade to the smallest cost value
        	cost[char1int][char2int] = Math.min(cost[char1int][char2int], tradeCost);
        	operations--;
        }
        
        //calling floydWarshall to find the shortest cost path
        floydWarshall();
        
        for (int i = 0; i < line1.length(); i++) {
        	//same name because they're the same thing, just in different loops
        	int char1int = line1.charAt(i) - 'a';
        	int char2int = line2.charAt(i) - 'a';
        	int lowerCost = 999;
        	if (char1int == char2int) continue; //skip this iteration
        	for (int j = 0; j < 26; j++) {
                //cost to change char1int with letter in position j	
        		int cost1 = cost[char1int][j];
        		//cost to change char2int with letter in position j
        		int cost2 = cost[char2int][j];
        		if (cost1 != 999 && cost2 != 999 && cost1 + cost2 < lowerCost) {
        			//lowerCost isn't actually the lower cost, so we need to update
        			lowerCost = cost1 + cost2;
        			//changing letters
        			line1.setCharAt(i, (char) (j + 'a'));
        			line2.setCharAt(i, (char) (j + 'a'));
        		}
        	}
        	answer += lowerCost;
        	}
        if (!line1.toString().equals(line2.toString())) {
        	System.out.println(-1);
        } else {
        	System.out.println(answer);
        	System.out.println(line1);
        }
	}
	static int cost[][] = new int [26][26];
	static void floydWarshall() {
		for (int k = 0; k < 26; k++)
			for (int i = 0; i < 26; i++)
				for (int j = 0; j < 26; j++) 
					cost[i][j] = Math.min(cost[i][j], cost[i][k] + cost[k][j]);
	}

}

class FastReader 
{ 
	BufferedReader br; 
	StringTokenizer st; 

	public FastReader() 
	{ 
		br = new BufferedReader(new
				InputStreamReader(System.in)); 
	} 

	String next() 
	{ 
		while (st == null || !st.hasMoreElements()) 
		{ 
			try
			{ 
				st = new StringTokenizer(br.readLine()); 
			} 
			catch (IOException e) 
			{ 
				e.printStackTrace(); 
			} 
		} 
		return st.nextToken(); 
	} 

	int nextInt() 
	{ 
		return Integer.parseInt(next()); 
	} 

	long nextLong() 
	{ 
		return Long.parseLong(next()); 
	} 

	double nextDouble() 
	{ 
		return Double.parseDouble(next()); 
	} 

	String nextLine() 
	{ 
		String str = ""; 
		try
		{ 
			str = br.readLine(); 
		} 
		catch (IOException e) 
		{ 
			e.printStackTrace(); 
		} 
		return str; 
	} 
}