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 StringProblem
{
	public Scanner in = new Scanner(System.in);
	public PrintStream out = System.out;
	
	public long oo = 90000000000L;
	public long[][] cost;
	
	public int[] A, B, C;
	public int n, E;
	
	public void main()
	{
		A = s2ia(in.next());
		B = s2ia(in.next());
		n = A.length;
		
		if(A.length != B.length)
		{
			out.println(-1);
			return;
		}
		
		C = new int[n];
		
		int i, j, k;
		
		cost = new long[26][26];
		init(cost, oo);
		for(i=0;i<26;++i) cost[i][i] = 0;
		
		E = in.nextInt();
		for(k=0;k<E;++k)
		{
			i = in.next().charAt(0) - 'a';
			j = in.next().charAt(0) - 'a';
			
			cost[i][j] = Math.min(cost[i][j], in.nextInt());
		}
		
		for(k=0;k<26;++k) for(i=0;i<26;++i) for(j=0;j<26;++j) cost[i][j] = Math.min(cost[i][j], cost[i][k] + cost[k][j]);
		
		int x;
		long sum = 0;
		for(i=0;i<n;++i)
		{
			long c = oo;
			for(x=0;x<26;++x)
			{
				if(cost[A[i]][x] + cost[B[i]][x] < c)
				{
					c = cost[A[i]][x] + cost[B[i]][x];
					C[i] = x;
				}
			}
			
			sum += c;
			if(sum >= oo) break;
		}
		
		if(sum < oo)
		{
			out.println(sum);
			out.println(new String(ia2ca(C)));
		}
		else
		{
			out.println(-1);
		}
	}//end public void main()
	
	public void init(long[][] A, long val)
	{ 
		for(int i=0;i<A.length;++i) Arrays.fill(A[i], val); 
	}

	public int[] s2ia(String s)
	{
		int[] ret = new int[s.length()];
		for(int i=0;i<ret.length;++i) ret[i] = (s.charAt(i) - 'a');
		return ret;
	}
	
	public char[] ia2ca(int[] v)
	{
		char[] res = new char[v.length];
		for(int i=0;i<v.length;++i) res[i] = (char)(v[i] + 'a');
		return res;
	}
	
	public static void main(String[] args)
	{
		(new StringProblem()).main();
	}
}