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();
		String s = fr.nextLine();
		String t = fr.nextLine();
		StringBuilder output = new StringBuilder();
		int n = fr.nextInt();
		int sTam = s.length();
		int tTam = t.length();
		
		if (sTam != tTam) {
			System.out.println(-1);
		}else {
			MatrizAdjPonderada grafo = new MatrizAdjPonderada(26);
			//n direcional
			for (int i = 0; i < n; i++) {
				String op = fr.nextLine();
				int opTam = op.length();
				char a = op.charAt(0);
				char b = op.charAt(2);			
				int w = Integer.valueOf(op.substring(4, opTam));
				grafo.add(a-97, b-97, w);
			}
			grafo.minimo(s, sTam, t, tTam, output);
			
		}


	}
	
	public static class MatrizAdjPonderada{
		private int[][] matriz;
		private int n;
		private int[][] distancias;
		private int[][] next;
		
		public MatrizAdjPonderada(int n) {
			matriz = new int[n][n];
			next = new int[n][n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (i == j) {
						matriz[i][j] = 0;
						next[i][j] = j;
					} else {
						matriz[i][j] = Integer.MAX_VALUE;
						next[i][j] = -1;
					}			
				}	
			}
			this.n = n;
		}
		
		public void minimo(String s, int sTam, String t, int tTam, StringBuilder output) {
			int minimo = 0;
			floyd();
			for (int i = 0; i < sTam; i++) {
				int letra1 = s.charAt(i)-97;
				int letra2 = t.charAt(i)-97;
				if (letra1 != letra2) {
					int letra3 = -1;
					int custoPossivel = Integer.MAX_VALUE;				
					for (int a = 0; a < 26; a++) {
						int peso1 = distancias[letra1][a];
						int peso2 = distancias[letra2][a];
						if (peso1 >= custoPossivel || peso2 >= custoPossivel) {
							//condicional que impede overflow
						} else {
							if (peso1 + peso2 < custoPossivel) {
								custoPossivel = peso1 + peso2;
								letra3 = a;
							}
						}

					}
					if (letra3 == -1) {
						minimo = -1;
						i = sTam;
					} else {
						output.append(toChar(letra3+97));
						//s.setCharAt(i, toChar(letra3+97));
						//t.setCharAt(i, toChar(letra3+97));
						minimo = minimo + custoPossivel;	
					}
							
				} else {				
					output.append(toChar(letra1+97));
				}
			}
			if (minimo >= 0) {
				System.out.println(minimo);
				System.out.print(output);
			}
			else {
				System.out.print(-1);
			}
			
		}

		public void floyd() {
			distancias = matriz;
			for (int k = 0; k < n; k++) {
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < n; j++) {
						if (distancias[i][k] != Integer.MAX_VALUE && distancias[k][j] != Integer.MAX_VALUE) {
							if (distancias[i][j] > distancias[i][k] + distancias[k][j]) {
								distancias[i][j] = distancias[i][k] + distancias[k][j];
								next [i][j] = next[i][k];
							}
						}
					}
				}
			}
		}
		
		public void add (int u, int v, int w) {
			if (matriz[u][v] > w) {
				matriz[u][v] = w;
				next[u][v] = v;
			}

		}
		
		public int first(int v) {
			for (int i = 0; i < n; i++) {
				if (matriz[v][i] != Integer.MAX_VALUE) {
					return i;
				}
			}
			return n;
		}
		
		public int next(int v, int w) {
			for (int i = w+1; i < n; i++) {
				if (matriz[v][i] != Integer.MAX_VALUE) {
					return i;
				}
			}
			return n;
		}

		public void print() {
			for (int i = 0; i < n; i++) {
				char v = toChar(i+97);
				System.out.print(v + ": ");
				for (int j = 0; j < n; j++) {
					if (matriz[i][j] != Integer.MAX_VALUE) {
						char c = toChar(j+97);
						System.out.print(c + "(" + matriz[i][j] + ")" + " ");
					}					
				}
				System.out.println("");
			}
		}
		
		public void printDistancias() {
			for (int i = 0; i < n; i++) {
				char v = toChar(i+97);
				System.out.print(v + ": ");
				for (int j = 0; j < n; j++) {
					if (distancias[i][j] != Integer.MAX_VALUE) {
						System.out.print(distancias[i][j] + " | ");
					}else {
						System.out.print(-10 + " | ");
					}
				}
				System.out.println("");
			}
		}
		
		
	}
	
	public static char toChar (int index) {
		return (char) index;
	}
	
	static 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()); 
        } 
  
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 

    }

}