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{
	static class FastReader{
		BufferedReader br;
		StringTokenizer st;
		public FastReader(){
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		public String next(){
			while(st == null || !st.hasMoreElements()){
				try {
					st = new StringTokenizer(br.readLine());
				}catch (IOException e){
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		public int nextInt(){
			return Integer.parseInt(next());
		}
	}
	public static void main(String[] args) {
		FastReader in = new FastReader();
		Graph graph = new Graph();
		StringBuilder a = new StringBuilder();
		String x = in.next();
		String y = in.next();
		int r=0;

		if (x.length() == y.length()) {
			int aux = in.nextInt();
			for(int i=0;i<aux;i++) {
				String v1 = in.next();
				String v2 = in.next();
				int weight = in.nextInt();
				graph.setEdge(v1.charAt(0), v2.charAt(0), weight);
			}
			int [][] matrix = graph.floydw();
			for (int i=0;i<x.length();i++) {
				int v1 = x.charAt(i) -'a';
				int v2 = y.charAt(i) -'a';
				int coust = matrix[v1][v2];
				char letter = (char) (v2+'a');
				if (v1!=v2) {
					if (matrix[v2][v1] < coust){
						coust = matrix[v2][v1];
						letter = (char) (v1+'a');
					}for(int j=0;j<26;j++) {
						if(matrix[v2][j]!=3000 && matrix[v1][j]!=3000) 
							if (matrix[v2][j]+matrix[v1][j]<coust) {
								coust = matrix[v2][j]+matrix[v1][j];
								letter = (char) (j+'a');

							}
					}
					if (coust == 3000) {
						r=-1;
						i=x.length();
					} else {
						r+=coust;
						a.append(letter);
					}
				} else a.append(letter);
			}
		} else r=-1;
		System.out.println(r);
		if(r != -1) System.out.println(a.toString());
	}
}
class Graph {
	private int [][] matrix;
	private boolean[] mark;

	public Graph() {
		this.matrix = new int [26][26];
		for (int i=0; i<26;i++) {
			for(int j=0;j<26;j++) {
				this.matrix[i][j] = 3000;
			}
			this.mark = new boolean[26];
		}
	}
	public void setEdge(int v1, int v2, int weight) {
		if(this.matrix[v1-'a'][v2-'a'] > weight) {
			this.matrix[v1-'a'][v2-'a'] = weight;
		}
	}
	public int [][] floydw() {
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++) {
				for (int k = 0; k < 26; k++) {
					if (this.matrix[j][k] > this.matrix[j][i]+ this.matrix[i][k]) {
						this.matrix[j][k] = this.matrix[j][i] + this.matrix[i][k];
					}	                       
				}
			}
		}
		return this.matrix;
	}
}