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.util.*;
import java.io.*;


public class Main {

	public static int comunMaisProx(int a, int b, int[][] D) {
		int posiDoMenor=-1;
		int menorValorSomado=Integer.MAX_VALUE;
		for (int i = 0; i < D.length; i++) {
			if (D[a][i]!=Integer.MAX_VALUE && D[b][i]!=Integer.MAX_VALUE) {
					if (D[a][i] + D[b][i] < menorValorSomado) {
						menorValorSomado=D[a][i]+D[b][i];
						posiDoMenor=i;
					}
			}
		}
		return posiDoMenor;
	}
	
	public static void main(String[] args) {

		FastReader in = new FastReader();

		String st1=in.nextLine();
		String st2=in.nextLine();
		
		GraphMatriz grafo= new GraphMatriz(26);
		
		boolean falhou=false;
		if (st1.length()!=st2.length()) {
			falhou=true;
		}
		
		int n=Integer.parseInt(in.nextLine());
		
		for (int i = 0; i < n; i++) {
			String aux= in.nextLine();
			int letra1=aux.charAt(0) -97;
			int letra2=aux.charAt(2) -97;
			grafo.setEdge(letra1, letra2, Integer.parseInt(aux.split(" ")[2]));
		}
		
		int[][] D=new int[26][26];
		D=grafo.floiyd(D);
		int total=0;
		StringBuffer sb=new StringBuffer();
		
		if (!falhou) {
			for (int i = 0; i < st1.length(); i++) {
				if (st1.charAt(i) != st2.charAt(i)) {
					char aux;
					int comunMaisProx = comunMaisProx(st1.charAt(i)-97,st2.charAt(i)-97,D);
						if (comunMaisProx == -1) {
							falhou = true;
							break;
						}else {
							total = total + D[st1.charAt(i)-97][comunMaisProx] + D[st2.charAt(i)-97][comunMaisProx];
							aux=(char) (comunMaisProx+97);
							sb.append(aux);
						}
				}else {
					sb.append(st1.charAt(i));
				}
			}
		}		
		if (falhou) {
			System.out.println(-1);
		}else {
			System.out.println(total);
			System.out.println(sb);
		}
	}

}

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;
	}
}

class GraphMatriz {
	int[][] matriz;

	public GraphMatriz(int n) {
		matriz = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				matriz[i][j] = -1;
			}
		}
	}

	public void setEdge(int v1, int v2, int peso) {// introduz a aresta
		if (matriz[v1][v2] == -1 || matriz[v1][v2] > peso) {
			matriz[v1][v2] = peso;
		}
	}

	public int[][] floiyd(int[][]D) {
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++) {
				if (i==j) {
					D[i][j] = 0;
				}else if (matriz[i][j] == -1){
					D[i][j] = Integer.MAX_VALUE;//infinito
				}
				else D[i][j] = matriz[i][j];
			}
		}
		for (int k = 0; k < 26; k++) {
			for (int i = 0; i < 26; i++) {
				for (int j = 0; j < 26; j++) {
					if (D[i][k]!=Integer.MAX_VALUE && D[k][j]!=Integer.MAX_VALUE && D[i][j]>D[i][k]+D[k][j]) {
						D[i][j]=D[i][k]+D[k][j];
					}
				}
			}
		}
		
		return D;
	}

	public int dist(int x, int y) {
		return matriz[x][y];
	}

}