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{
    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()); 
    } 
 
    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; 
    } 
}
    
public static void main(String[] args) {
		FastReader in = new FastReader ();
		L6QB grafo = new L6QB(26);
		int[][] distancias = new int[26][26];
		int menorc=99999999;
		//inicializando distancias com 999	
		for(int i=0; i<26; i++) {
			for(int j=0; j<26; j++) {
				distancias[i][j] = 99999999;
			}
		}
		String A = in.nextLine();
		String igual = "";
		char carac = '0';
		int total = 0;
		String B = in.nextLine();
		int qtd = in.nextInt();
		//adicionando arestas
		for(int i = 0; i<qtd; i++) {
			String linha = in.nextLine();
			
			int c1 = linha.charAt(0);
			int c2 = linha.charAt(2);
			int peso = Integer.parseInt(linha.split(" ")[2]);
			
			grafo.addEdge(c1-97, c2-97, peso);
		}
		
		//encontrando a menor distancia
		L6QB.floydWarshall(grafo, distancias);
		
		if(A.length()!=B.length()) {
			System.out.println(-1);
		} else {
			
			for(int i = 0; i< A.length(); i++) {
				int carac1 = A.charAt(i);
				int carac2 = B.charAt(i);
				if(carac1 != carac2) {					
					
					for(int j=0; j<distancias.length; j++) {
						if((distancias[carac1-97][j] != 99999999 && distancias[carac2-97][j] != 99999999) && distancias[carac1-97][j] + distancias[carac2-97][j] < menorc) {
							menorc = distancias[carac1-97][j] + distancias[carac2-97][j];
							carac = (char) (j+97);							
						}
					}
					
				if(carac != '0') {
					total = total + menorc;
					igual = igual + carac;
					} else {
					    total = 99999999;
					    break;
					}
					carac = '0';
					menorc =99999999;
				} else {
					igual = igual + A.charAt(i);
				}
			}
			
			if(total<99999999) {
				System.out.println(total);
				System.out.println(igual);
				} else {
					System.out.println(-1);
				}
				
			
			
		}
 
	}
 
}
 
class L6QB {
	public static int[][] adjacencyList;
	int numEdges;;
 
	
	public L6QB(int n){
		
		this.numEdges = 0;
		adjacencyList = new int[n][n];
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				adjacencyList[i][j] = 99999999;
			}
		}
	}
  /*
    @Method:add the specific edge
    @Description:use this funtion to construct a graph
  */ 
	public void addEdge(int i,int j, int k){
		if(adjacencyList[i][j] > k) {
			adjacencyList[i][j] = k;
		}
		numEdges++;
		
	}
	
	
	 static void floydWarshall(L6QB G, int[][] D) { 
	        for (int i = 0; i < adjacencyList.length; i++) 
	            for (int j = 0; j < adjacencyList.length; j++) 
	                if(i==j) {
	                	D[i][j] = 0;
	                } else if(G.weight(i,j) != 99999999) {
	                	D[i][j] = G.weight(i,j);
	                }else {
	                	D[i][j] = 99999999;
	                }
	        for (int k = 0; k < adjacencyList.length; k++) { 
 
	            for (int i = 0; i < adjacencyList.length; i++) { 
 
	                for (int j = 0; j < adjacencyList.length; j++) { 
	                	if(D[i][j]>(D[i][k]+D[k][j]) ) {
	                		D[i][j] = D[i][k] + D[k][j];
	                	}
	                	
	                } 
	            } 
	        } 	  
	      
	    } 
	  
 
 
	public int weight(int i, int j) {
		return adjacencyList[i][j];
		}
}