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) {
		
		FastReader in = new FastReader();
		Grafo grafo = new Grafo(26);
		StringBuilder palavra1 = new StringBuilder(in.nextLine());
		StringBuilder palavra2 = new StringBuilder(in.nextLine());
		if(palavra1.length()!=palavra2.length()) {
			System.out.println(-1);
			return;
		}
		int arestas = in.nextInt();
		boolean[][] marcados = new boolean [26][26];
		for(int i = 0; i < arestas; i ++) {
			String linha = in.nextLine();
			int vertice1 = linha.charAt(0) - 'a';
			int vertice2 = linha.charAt(2) - 'a';
			int peso = Integer.parseInt(linha.split(" ")[2]);
			grafo.setEdge(vertice1, vertice2, peso, marcados);
		}
		int total = 0;
		int [][] distancias = new int [26][26];
		grafo.floyd(distancias);
		for(int i=0; i<palavra1.length(); i++) {
			int letra1 = palavra1.charAt(i) - 'a';
			int letra2 = palavra2.charAt(i) - 'a';
			if(letra1!=letra2) {
				long aux = 6000;
				for(int j=0; j<26; j++) {
					int valor1 = distancias[letra1][j];
					int valor2 = distancias[letra2][j];
					long soma = valor1+valor2;
					if(soma<aux && valor1!=6000 && valor2!=6000) {
						aux=soma;
						palavra1.setCharAt(i, (char)(j+'a'));
						palavra2.setCharAt(i, (char)(j+'a'));
					}
				}
				total+=aux;
			}
		}
		if(!palavra1.toString().equals(palavra2.toString())) {
			System.out.println(-1);
		} else {
			System.out.println(total + "\n" + palavra2);
		}
	}

}

class Grafo {
	int[][] matriz;
	int numArestas;
	boolean[] marcados; //array de marcados
	
	public Grafo(int n) {
		this.matriz = new int[n][n];
		this.numArestas = 0;
		this.marcados = new boolean[n];
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				this.matriz[i][j]=-1;
			}
		}
	}
	
	public int first(int vertice) {
		for(int i=0; i<matriz.length; i++) {
			if(this.matriz[vertice][i]!=0) {
				return i;
			}
		}
		return this.matriz.length;
	}
	
	public int next (int vertice1, int vertice2) {
		for(int i=(vertice2+1); i<matriz.length; i++) {
			if(this.matriz[vertice1][i]!=0) {
				return i;
			}
		}
		return this.matriz.length;
	}
	
	void setEdge (int vertice1, int vertice2, int peso, boolean[][] marcados) {
		if(this.matriz[vertice1][vertice2]==-1 && !marcados[vertice1][vertice2]) {
			this.numArestas++;
			this.matriz[vertice1][vertice2] = peso;
			marcados[vertice1][vertice2] = true;
		} else if(marcados[vertice1][vertice2] && this.matriz[vertice1][vertice2]>peso) {
			this.matriz[vertice1][vertice2]=peso;
		}
	}
	
	public int weight (int vertice1, int vertice2) {
		return this.matriz[vertice1][vertice2];
	}
	
	public void floyd (int[][] distancias) {
		for(int i=0; i<this.matriz.length; i++) {
			for(int j=0; j<this.matriz.length; j++) {
				if(i==j) {
					distancias[i][j] = 0;
				} else if (weight(i, j)!=-1) {
					distancias[i][j]=weight(i, j);
				} else {
					distancias[i][j]=6000;
				}
			}
		}
		for(int k=0; k<this.matriz.length; k++) {
			for(int i=0; i<this.matriz.length; i++) {
				for(int j=0; j<this.matriz.length; j++) {
					if(distancias[i][k]!=6000 && distancias[k][j]!=6000 && distancias[i][j]>(distancias[i][k]+distancias[k][j])) {
						distancias[i][j] = distancias[i][k]+distancias[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; 
    } 
}