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.util.Map.Entry;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;


public class CF {
	private static FS sc = new FS();
	
	private static class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());				               
	            } catch (IOException e) {}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
	
	private static class extra {
		
		static int[] intArr(int size) {
			int[] a = new int[size];
			for(int i = 0; i < size; i++) a[i] = sc.nextInt();
			return a;
		}
		
		static long[] longArr(int size) {
			long[] a = new long[size];
			for(int i = 0; i < size; i++) a[i] = sc.nextLong();
			return a;
		}
		
		static long intSum(int[] a) {
			long sum = 0; 
			for(int i = 0; i < a.length; i++) {
				sum += a[i];
			}
			return sum;
		}
		
		static long longSum(long[] a) {
			long sum = 0; 
			for(int i = 0; i < a.length; i++) {
				sum += a[i];
			}
			return sum;
		}
		
		static LinkedList[] graphD(int vertices, int edges) {
			LinkedList<Integer>[] temp = new LinkedList[vertices+1];
			for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
			for(int i = 0; i < edges; i++) {
				int x = sc.nextInt();
				int y = sc.nextInt();
				temp[x].add(y);
			}
			return temp;
		}
		
		static LinkedList[] graphUD(int vertices, int edges) {
			LinkedList<Integer>[] temp = new LinkedList[vertices+1];
			for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
			for(int i = 0; i < edges; i++) {
				int x = sc.nextInt();
				int y = sc.nextInt();
				temp[x].add(y);
				temp[y].add(x);
			}
			return temp;
		}
		
		static void printG(LinkedList[] temp) {
			for(LinkedList<Integer> aa:temp) System.out.println(aa);
		}
		
		static long cal(long val, long pow) {
			if(pow == 0) return 1;
			long res = cal(val, pow/2);
			
//			long ret = (res*res)%mod;
//			if(pow%2 == 0) return ret;
//			return (val*ret)%mod;
			long ret = res*res;
			if(pow%2 == 0) return ret;
			return val*ret;
		}
		
		static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); }
		
	}
	
	static int mod = (int) 1e9 + 9;
//	static int mod = (int) 998244353;
	static int max = (int) 1e5+5, sq = 316;
	static LinkedList<pair>[] temp;
	static int[] par, rank;
	
	static class pair implements Comparator<pair>{
		char v;
		int w;
		pair(char x, int y) { v = x; w = y; }
		pair() {}
		
		public int compare(pair o1, pair o2) {
			return o1.w - o2.w;
		}
	}
	
	public static void main(String[] args) {	
//		int t = sc.nextInt();
		int t = 1;
		StringBuilder ret = new StringBuilder();
		while(t-- > 0) {
			String a = sc.next();
			String b = sc.next();
			temp = new LinkedList[26];
			if(a.length() != b.length()) {
				ret.append(-1 + "\n");
				continue;
			}
			for(int i = 0; i < 26; i++) temp[i] = new LinkedList<>();
			int[][] dis = new int[26][26];
			for(int i = 0; i < 26; i++) Arrays.fill(dis[i], (int)1e9);
			int q = sc.nextInt();
			for(int i = 0; i < q; i++) {
				char from = sc.next().charAt(0);
				char to = sc.next().charAt(0);
				int w = sc.nextInt();
				temp[from-'a'].add(new pair(to, w));
//				dis[from-'a'][to-'a'] = Math.min(dis[from-'a'][to-'a'], w);
			}
			for(int i = 0; i < 26; i++) {
				PriorityQueue<pair> pq = new PriorityQueue<>(new pair());
				pq.add(new pair((char)(i+'a'), 0));
				if(dis[i][i] == 1e9) dis[i][i] = 0;
				while(!pq.isEmpty()) {
					pair cur = pq.poll();
					for(pair aa:temp[cur.v-'a']) {
						if(aa.w + dis[i][cur.v-'a'] < dis[i][aa.v-'a']) {
							dis[i][aa.v-'a'] = aa.w + dis[i][cur.v-'a'];
							pq.add(new pair(aa.v, dis[i][aa.v-'a']));
						}
					}
				}
			}
//			for(int i = 0; i < 26; i++) {
//				for(int j = 0; j < 26; j++) System.out.print(dis[i][j] + " ");
//				System.out.println();
//			}
			StringBuilder fin = new StringBuilder();
			int flag = 0, ans = 0;
			for(int i = 0; i < a.length(); i++) {
				if(a.charAt(i) == b.charAt(i)) fin.append(a.charAt(i));
				else {
					char min = 'a';
					int value = (int)(1e9), curf = 0;
					for(int j = 0; j < 26; j++) {
//						System.out.println(dis[a.charAt(i)-'a'][j]+ " " + dis[b.charAt(i)-'a'][j] + " " + a.charAt(i) + " " + b.charAt(i) + " " + (char)(j+'a'));
						if(value > dis[a.charAt(i)-'a'][j]+dis[b.charAt(i)-'a'][j]) {
							value = dis[a.charAt(i)-'a'][j]+dis[b.charAt(i)-'a'][j];
							min = (char)(j+'a');
							curf = 1;
						}
					}
					if(curf == 0) flag = 1;
					fin.append(min);
					ans += value;
				}
			}
			ret.append(flag == 1 ? -1:ans + "\n" + fin + "\n");
 		}
		System.out.println(ret);
	}
	
//	static void dfs(char ch) {
//		
//	}
	
}